@Override public SearchResponse<Group> search(SearchRequest request) throws SecurityManagementException { MemoryUserDatabase userDatabase = getDatabase(); try { Iterator<org.apache.catalina.Role> groups = userDatabase.getRoles(); Collection<String> groupIdentifiers = new ArrayList<String>(); if (groups != null) { while (groups.hasNext()) { org.apache.catalina.Role group = groups.next(); String groupname = group.getRolename(); groupIdentifiers.add(groupname); } } return groupsSearchEngine.searchByIdentifiers(groupIdentifiers, request); } finally { closeDatabase(userDatabase); } }
@Override public Group create(Group entity) throws SecurityManagementException { if (entity == null) { throw new NullPointerException(); } MemoryUserDatabase userDatabase = getDatabase(); try { String name = entity.getName(); userDatabase.createRole(name, name); saveDatabase(userDatabase); return entity; } finally { closeDatabase(userDatabase); } }
@Override public void delete(String... identifiers) throws SecurityManagementException { if (identifiers == null) { throw new NullPointerException(); } MemoryUserDatabase userDatabase = getDatabase(); try { for (String identifier : identifiers) { org.apache.catalina.Role group = getRole(userDatabase, identifier); userDatabase.removeRole(group); } saveDatabase(userDatabase); } finally { closeDatabase(userDatabase); } }
@Override public void assignUsers(String name, Collection<String> users) throws SecurityManagementException { if (name == null) { throw new NullPointerException(); } if (users != null) { MemoryUserDatabase userDatabase = getDatabase(); org.apache.catalina.Role role = getRole(userDatabase, name); try { for (String username : users) { org.apache.catalina.User user = getUser(userDatabase, username); user.addRole(role); } saveDatabase(userDatabase); } finally { closeDatabase(userDatabase); } } }
@Override public SearchResponse<User> search(SearchRequest request) throws SecurityManagementException { MemoryUserDatabase userDatabase = getDatabase(); try { Iterator<org.apache.catalina.User> users = userDatabase.getUsers(); Collection<String> userIdentifiers = new ArrayList<String>(); if (users != null) { while (users.hasNext()) { org.apache.catalina.User user = users.next(); String username = user.getUsername(); userIdentifiers.add(username); } } return usersSearchEngine.searchByIdentifiers(userIdentifiers, request); } finally { closeDatabase(userDatabase); } }
@Override public User get(String identifier) throws SecurityManagementException { MemoryUserDatabase userDatabase = getDatabase(); try { org.apache.catalina.User user = getUser(userDatabase, identifier); Iterator<Role> groups = user.getRoles(); User u = createUser(user, groups); u.setProperty(ATTRIBUTE_USER_FULLNAME, user.getFullName() != null ? user.getFullName() : ""); return u; } finally { closeDatabase(userDatabase); } }
@Override public User create(User entity) throws SecurityManagementException { checkNotNull("entity", entity); MemoryUserDatabase userDatabase = getDatabase(); try { String username = entity.getIdentifier(); String fullName = entity.getProperty(ATTRIBUTE_USER_FULLNAME); userDatabase.createUser(username, "", fullName != null ? fullName : ""); saveDatabase(userDatabase); return entity; } finally { closeDatabase(userDatabase); } }
@Override public User update(User entity) throws SecurityManagementException { checkNotNull("entity", entity); MemoryUserDatabase userDatabase = getDatabase(); try { org.apache.catalina.User user = getUser(userDatabase, entity.getIdentifier()); String fName = entity.getProperty(ATTRIBUTE_USER_FULLNAME); user.setFullName(fName != null ? fName : ""); saveDatabase(userDatabase); return entity; } finally { closeDatabase(userDatabase); } }
@Override public void delete(String... identifiers) throws SecurityManagementException { checkNotNull("identifiers", identifiers); MemoryUserDatabase userDatabase = getDatabase(); try { for (String identifier : identifiers) { org.apache.catalina.User user = getUser(userDatabase, identifier); userDatabase.removeUser(user); } saveDatabase(userDatabase); } finally { closeDatabase(userDatabase); } }
private void doAssignGroups(String username, Collection<String> ids) throws SecurityManagementException { MemoryUserDatabase userDatabase = getDatabase(); try { org.apache.catalina.User user = getUser(userDatabase, username); user.removeRoles(); if (!ids.isEmpty()) { for (String roleName : ids) { org.apache.catalina.Role role = getRole(userDatabase, roleName); user.addRole(role); } } saveDatabase(userDatabase); } finally { closeDatabase(userDatabase); } }
@Override public void changePassword(String username, String newPassword) throws SecurityManagementException { checkNotNull("username", username); MemoryUserDatabase userDatabase = getDatabase(); try { org.apache.catalina.User user = getUser(userDatabase, username); user.setPassword(newPassword); saveDatabase(userDatabase); } finally { closeDatabase(userDatabase); } }
public void start(final StandardServer server) { if (SystemInstance.get().isDefaultProfile()) { // add user tomee is no user are specified try { final NamingResourcesImpl resources = server.getGlobalNamingResources(); final ContextResource userDataBaseResource = resources.findResource("UserDatabase"); final UserDatabase db = (UserDatabase) server.getGlobalNamingContext().lookup(userDataBaseResource.getName()); if (!db.getUsers().hasNext() && db instanceof MemoryUserDatabase) { final MemoryUserDatabase mudb = (MemoryUserDatabase) db; final boolean oldRo = mudb.getReadonly(); try { ((MemoryUserDatabase) db).setReadonly(false); db.createRole("tomee-admin", "tomee admin role"); db.createUser("tomee", "tomee", "TomEE"); db.findUser("tomee").addRole(db.findRole("tomee-admin")); } finally { mudb.setReadonly(oldRo); } } } catch (final Throwable t) { // no-op } } }
@Override public Group get(String identifier) throws SecurityManagementException { MemoryUserDatabase userDatabase = getDatabase(); try { org.apache.catalina.Role group = getRole(userDatabase, identifier); return createGroup(group); } finally { closeDatabase(userDatabase); } }
protected MemoryUserDatabase getDatabase() throws SecurityManagementException { MemoryUserDatabase database = new MemoryUserDatabase(DATABASE_NAME); database.setPathname(usersFile); database.setReadonly(false); try { database.open(); if (!database.getReadonly()) { database.save(); } } catch (Exception e) { throw new SecurityManagementException(e); } return (database); }
protected void saveDatabase(MemoryUserDatabase database) throws SecurityManagementException { try { database.save(); } catch (Exception e) { throw new SecurityManagementException(e); } }
protected void closeDatabase(MemoryUserDatabase database) throws SecurityManagementException { try { database.close(); } catch (Exception e) { throw new SecurityManagementException(e); } }
protected org.apache.catalina.User getUser(MemoryUserDatabase database, String identifier) { org.apache.catalina.User user = database.findUser(identifier); if (user == null) { throw new UserNotFoundException(identifier); } return user; }
protected org.apache.catalina.Role getRole(MemoryUserDatabase database, String identifier) { org.apache.catalina.Role group = database.findRole(identifier); if (group == null) { throw new GroupNotFoundException(identifier); } return group; }
@Test public void testChangePassword() { usersManager.changePassword("user1", "newUser1Password"); MemoryUserDatabase database = usersManager.getDatabase(); org.apache.catalina.User catalinaUser = usersManager.getUser(database, "user1"); assertEquals("newUser1Password", catalinaUser.getPassword()); }
public void createDatabase(String path) { database = new MemoryUserDatabase(name); ((MemoryUserDatabase) database).setPathname(path); try { database.open(); } catch (Exception e) { } }