Search in sources :

Example 1 with IniRealm

use of org.apache.shiro.realm.text.IniRealm in project zeppelin by apache.

the class SecurityRestApi method getUserList.

/**
   * Get userlist
   * Returns list of all user from available realms
   *
   * @return 200 response
   */
@GET
@Path("userlist/{searchText}")
public Response getUserList(@PathParam("searchText") final String searchText) {
    List<String> usersList = new ArrayList<>();
    List<String> rolesList = new ArrayList<>();
    try {
        GetUserList getUserListObj = new GetUserList();
        Collection realmsList = SecurityUtils.getRealmsList();
        if (realmsList != null) {
            for (Iterator<Realm> iterator = realmsList.iterator(); iterator.hasNext(); ) {
                Realm realm = iterator.next();
                String name = realm.getClass().getName();
                if (LOG.isDebugEnabled()) {
                    LOG.debug("RealmClass.getName: " + name);
                }
                if (name.equals("org.apache.shiro.realm.text.IniRealm")) {
                    usersList.addAll(getUserListObj.getUserList((IniRealm) realm));
                    rolesList.addAll(getUserListObj.getRolesList((IniRealm) realm));
                } else if (name.equals("org.apache.zeppelin.realm.LdapGroupRealm")) {
                    usersList.addAll(getUserListObj.getUserList((JndiLdapRealm) realm, searchText));
                } else if (name.equals("org.apache.zeppelin.realm.LdapRealm")) {
                    usersList.addAll(getUserListObj.getUserList((LdapRealm) realm, searchText));
                    rolesList.addAll(getUserListObj.getRolesList((LdapRealm) realm));
                } else if (name.equals("org.apache.zeppelin.realm.ActiveDirectoryGroupRealm")) {
                    usersList.addAll(getUserListObj.getUserList((ActiveDirectoryGroupRealm) realm, searchText));
                } else if (name.equals("org.apache.shiro.realm.jdbc.JdbcRealm")) {
                    usersList.addAll(getUserListObj.getUserList((JdbcRealm) realm));
                }
            }
        }
    } catch (Exception e) {
        LOG.error("Exception in retrieving Users from realms ", e);
    }
    List<String> autoSuggestUserList = new ArrayList<>();
    List<String> autoSuggestRoleList = new ArrayList<>();
    Collections.sort(usersList);
    Collections.sort(rolesList);
    Collections.sort(usersList, new Comparator<String>() {

        @Override
        public int compare(String o1, String o2) {
            if (o1.matches(searchText + "(.*)") && o2.matches(searchText + "(.*)")) {
                return 0;
            } else if (o1.matches(searchText + "(.*)")) {
                return -1;
            }
            return 0;
        }
    });
    int maxLength = 0;
    for (String user : usersList) {
        if (StringUtils.containsIgnoreCase(user, searchText)) {
            autoSuggestUserList.add(user);
            maxLength++;
        }
        if (maxLength == 5) {
            break;
        }
    }
    for (String role : rolesList) {
        if (StringUtils.containsIgnoreCase(role, searchText)) {
            autoSuggestRoleList.add(role);
        }
    }
    Map<String, List> returnListMap = new HashMap<>();
    returnListMap.put("users", autoSuggestUserList);
    returnListMap.put("roles", autoSuggestRoleList);
    return new JsonResponse<>(Response.Status.OK, "", returnListMap).build();
}
Also used : IniRealm(org.apache.shiro.realm.text.IniRealm) JdbcRealm(org.apache.shiro.realm.jdbc.JdbcRealm) LdapRealm(org.apache.zeppelin.realm.LdapRealm) JndiLdapRealm(org.apache.shiro.realm.ldap.JndiLdapRealm) IniRealm(org.apache.shiro.realm.text.IniRealm) LdapRealm(org.apache.zeppelin.realm.LdapRealm) Realm(org.apache.shiro.realm.Realm) ActiveDirectoryGroupRealm(org.apache.zeppelin.realm.ActiveDirectoryGroupRealm) JdbcRealm(org.apache.shiro.realm.jdbc.JdbcRealm) JndiLdapRealm(org.apache.shiro.realm.ldap.JndiLdapRealm) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 2 with IniRealm

use of org.apache.shiro.realm.text.IniRealm in project zeppelin by apache.

the class SecurityUtils method getRoles.

/**
   * Return the roles associated with the authenticated user if any otherwise returns empty set
   * TODO(prasadwagle) Find correct way to get user roles (see SHIRO-492)
   *
   * @return shiro roles
   */
public static HashSet<String> getRoles() {
    if (!isEnabled) {
        return EMPTY_HASHSET;
    }
    Subject subject = org.apache.shiro.SecurityUtils.getSubject();
    HashSet<String> roles = new HashSet<>();
    Map allRoles = null;
    if (subject.isAuthenticated()) {
        Collection realmsList = SecurityUtils.getRealmsList();
        for (Iterator<Realm> iterator = realmsList.iterator(); iterator.hasNext(); ) {
            Realm realm = iterator.next();
            String name = realm.getClass().getName();
            if (name.equals("org.apache.shiro.realm.text.IniRealm")) {
                allRoles = ((IniRealm) realm).getIni().get("roles");
                break;
            } else if (name.equals("org.apache.zeppelin.realm.LdapRealm")) {
                allRoles = ((LdapRealm) realm).getListRoles();
                break;
            }
        }
        if (allRoles != null) {
            Iterator it = allRoles.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry pair = (Map.Entry) it.next();
                if (subject.hasRole((String) pair.getKey())) {
                    roles.add((String) pair.getKey());
                }
            }
        }
    }
    return roles;
}
Also used : IniRealm(org.apache.shiro.realm.text.IniRealm) Subject(org.apache.shiro.subject.Subject) Iterator(java.util.Iterator) Collection(java.util.Collection) LdapRealm(org.apache.zeppelin.realm.LdapRealm) Map(java.util.Map) IniRealm(org.apache.shiro.realm.text.IniRealm) LdapRealm(org.apache.zeppelin.realm.LdapRealm) Realm(org.apache.shiro.realm.Realm) HashSet(java.util.HashSet)

Example 3 with IniRealm

use of org.apache.shiro.realm.text.IniRealm in project killbill by killbill.

the class IniRealmProvider method get.

@Override
public IniRealm get() {
    try {
        final Factory<SecurityManager> factory = new IniSecurityManagerFactory(securityConfig.getShiroResourcePath());
        // TODO Pierre hack - lame cast here, but we need to have Shiro go through its reflection magic
        // to parse the [main] section of the ini file. Without duplicating code, this seems to be possible only
        // by going through IniSecurityManagerFactory.
        final DefaultSecurityManager securityManager = (DefaultSecurityManager) factory.getInstance();
        final Collection<Realm> realms = securityManager.getRealms();
        IniRealm iniRealm = null;
        if (realms == null || realms.isEmpty()) {
            iniRealm = new IniRealm(securityConfig.getShiroResourcePath());
        } else {
            for (final Realm cur : realms) {
                if (cur instanceof IniRealm) {
                    iniRealm = (IniRealm) cur;
                    break;
                }
            }
        }
        if (iniRealm != null) {
            // See JavaDoc warning: https://shiro.apache.org/static/1.2.3/apidocs/org/apache/shiro/realm/AuthenticatingRealm.html
            iniRealm.setAuthenticationCachingEnabled(true);
            return iniRealm;
        } else {
            throw new ConfigurationException();
        }
    } catch (final ConfigurationException e) {
        log.warn("Unable to configure RBAC", e);
        return new IniRealm();
    }
}
Also used : IniSecurityManagerFactory(org.apache.shiro.config.IniSecurityManagerFactory) DefaultSecurityManager(org.apache.shiro.mgt.DefaultSecurityManager) SecurityManager(org.apache.shiro.mgt.SecurityManager) ConfigurationException(org.apache.shiro.config.ConfigurationException) IniRealm(org.apache.shiro.realm.text.IniRealm) DefaultSecurityManager(org.apache.shiro.mgt.DefaultSecurityManager) IniRealm(org.apache.shiro.realm.text.IniRealm) Realm(org.apache.shiro.realm.Realm)

Aggregations

Realm (org.apache.shiro.realm.Realm)3 IniRealm (org.apache.shiro.realm.text.IniRealm)3 LdapRealm (org.apache.zeppelin.realm.LdapRealm)2 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 ConfigurationException (org.apache.shiro.config.ConfigurationException)1 IniSecurityManagerFactory (org.apache.shiro.config.IniSecurityManagerFactory)1 DefaultSecurityManager (org.apache.shiro.mgt.DefaultSecurityManager)1 SecurityManager (org.apache.shiro.mgt.SecurityManager)1 JdbcRealm (org.apache.shiro.realm.jdbc.JdbcRealm)1 JndiLdapRealm (org.apache.shiro.realm.ldap.JndiLdapRealm)1 Subject (org.apache.shiro.subject.Subject)1 ActiveDirectoryGroupRealm (org.apache.zeppelin.realm.ActiveDirectoryGroupRealm)1