Search in sources :

Example 1 with UserRemovedFromProject

use of org.bimserver.models.log.UserRemovedFromProject in project BIMserver by opensourceBIM.

the class RemoveUserFromProjectDatabaseAction method execute.

@Override
public Boolean execute() throws UserException, BimserverDatabaseException, BimserverLockConflictException {
    User user = getUserByUoid(uoid);
    Project project = getProjectByPoid(poid);
    User actingUser = getUserByUoid(authorization.getUoid());
    if (authorization.hasRightsOnProject(actingUser, project)) {
        if (user.getUserType() == UserType.ADMIN) {
            int nrAdmins = 0;
            for (User authUser : project.getHasAuthorizedUsers()) {
                if (authUser.getUserType() == UserType.ADMIN) {
                    nrAdmins++;
                }
            }
            if (nrAdmins == 1) {
                throw new UserException("User cannot be removed from this project because it is the only admin user with authorization on this project");
            }
        }
        project.getHasAuthorizedUsers().remove(user);
        user.getHasRightsOn().remove(project);
        final UserRemovedFromProject userRemovedFromProject = getDatabaseSession().create(UserRemovedFromProject.class);
        userRemovedFromProject.setDate(new Date());
        userRemovedFromProject.setExecutor(actingUser);
        userRemovedFromProject.setAccessMethod(getAccessMethod());
        userRemovedFromProject.setProject(project);
        userRemovedFromProject.setUser(user);
        getDatabaseSession().addPostCommitAction(new PostCommitAction() {

            @Override
            public void execute() throws UserException {
                bimServer.getNotificationsManager().notify(new SConverter().convertToSObject(userRemovedFromProject));
            }
        });
        getDatabaseSession().store(user);
        getDatabaseSession().store(project);
        return true;
    } else {
        throw new UserException("Insufficient rights to remove user from project");
    }
}
Also used : Project(org.bimserver.models.store.Project) UserRemovedFromProject(org.bimserver.models.log.UserRemovedFromProject) User(org.bimserver.models.store.User) SConverter(org.bimserver.interfaces.SConverter) PostCommitAction(org.bimserver.database.PostCommitAction) UserException(org.bimserver.shared.exceptions.UserException) UserRemovedFromProject(org.bimserver.models.log.UserRemovedFromProject) Date(java.util.Date)

Aggregations

Date (java.util.Date)1 PostCommitAction (org.bimserver.database.PostCommitAction)1 SConverter (org.bimserver.interfaces.SConverter)1 UserRemovedFromProject (org.bimserver.models.log.UserRemovedFromProject)1 Project (org.bimserver.models.store.Project)1 User (org.bimserver.models.store.User)1 UserException (org.bimserver.shared.exceptions.UserException)1