Search in sources :

Example 1 with UserPermission

use of org.activityinfo.server.database.hibernate.entity.UserPermission in project activityinfo by bedatadriven.

the class GetSyncRegionsHandler method execute.

@Override
public SyncRegions execute(GetSyncRegions cmd, User user) {
    Set<Integer> countryIds = Sets.newHashSet();
    Set<Integer> visibleDatabaseIds = Sets.newHashSet();
    List<SyncRegion> databaseRegions = new ArrayList<>();
    List<Database> ownedDatabases = entityManager.createQuery("SELECT db FROM Database db WHERE db.owner = :user", Database.class).setParameter("user", user).getResultList();
    for (Database database : ownedDatabases) {
        if (!database.isDeleted()) {
            visibleDatabaseIds.add(database.getId());
            countryIds.add(database.getCountry().getId());
        }
        databaseRegions.add(new SyncRegion("db/" + database.getId(), database.getVersion()));
    }
    List<UserPermission> sharedDatabases = entityManager.createQuery("SELECT p FROM UserPermission p LEFT JOIN FETCH p.database WHERE p.user = :user", UserPermission.class).setParameter("user", user).getResultList();
    for (UserPermission permission : sharedDatabases) {
        if (permission.isAllowView() && !permission.getDatabase().isDeleted()) {
            visibleDatabaseIds.add(permission.getDatabase().getId());
            countryIds.add(permission.getDatabase().getCountry().getId());
        }
        long version = Math.max(permission.getVersion(), permission.getDatabase().getVersion());
        databaseRegions.add(new SyncRegion("db/" + permission.getDatabase().getId(), version));
    }
    List<SyncRegion> regions = Lists.newArrayList();
    regions.add(new SyncRegion("site-tables", TableDefinitionUpdateBuilder.CURRENT_VERSION));
    for (Integer countryId : countryIds) {
        regions.add(new SyncRegion("country/" + countryId, "1"));
    }
    regions.addAll(databaseRegions);
    regions.addAll(listAdminRegions(countryIds));
    regions.addAll(listLocations(countryIds));
    regions.addAll(listSiteRegions(visibleDatabaseIds));
    return new SyncRegions(regions);
}
Also used : SyncRegion(org.activityinfo.legacy.shared.command.result.SyncRegion) ArrayList(java.util.ArrayList) Database(org.activityinfo.server.database.hibernate.entity.Database) SyncRegions(org.activityinfo.legacy.shared.command.result.SyncRegions) GetSyncRegions(org.activityinfo.legacy.shared.command.GetSyncRegions) UserPermission(org.activityinfo.server.database.hibernate.entity.UserPermission)

Example 2 with UserPermission

use of org.activityinfo.server.database.hibernate.entity.UserPermission in project activityinfo by bedatadriven.

the class GetUsersHandler method execute.

@Override
public CommandResult execute(GetUsers cmd, User currentUser) {
    Database db = em.getReference(Database.class, cmd.getDatabaseId());
    UserPermission currentUserPermission = PermissionOracle.using(em).getPermissionByUser(db, currentUser);
    assertAuthorized(currentUserPermission);
    String whereClause = "up.database.id = :dbId and " + "up.user.id <> :currentUserId and " + "up.allowView = true";
    if (!currentUserPermission.isAllowManageAllUsers()) {
        whereClause += " and up.partner.id = " + currentUserPermission.getPartner().getId();
    }
    TypedQuery<UserPermission> query = em.createQuery("select up from UserPermission up where " + whereClause + " " + composeOrderByClause(cmd), UserPermission.class).setParameter("dbId", cmd.getDatabaseId()).setParameter("currentUserId", currentUser.getId());
    List<Folder> folders = em.createQuery("select f from Folder f where f.database.id = :dbId", Folder.class).setParameter("dbId", cmd.getDatabaseId()).getResultList();
    Map<ResourceId, Folder> folderMap = new HashMap<>();
    for (Folder folder : folders) {
        folderMap.put(CuidAdapter.folderId(folder.getId()), folder);
    }
    if (cmd.getOffset() > 0) {
        query.setFirstResult(cmd.getOffset());
    }
    if (cmd.getLimit() > 0) {
        query.setMaxResults(cmd.getLimit());
    }
    List<UserPermissionDTO> models = new ArrayList<>();
    for (UserPermission perm : query.getResultList()) {
        UserPermissionDTO dto = new UserPermissionDTO();
        dto.setEmail(perm.getUser().getEmail());
        dto.setName(perm.getUser().getName());
        dto.setOrganization(perm.getUser().getOrganization());
        dto.setJobtitle(perm.getUser().getJobtitle());
        dto.setAllowDesign(perm.isAllowDesign());
        dto.setAllowView(perm.isAllowView());
        dto.setAllowViewAll(perm.isAllowViewAll());
        dto.setAllowEdit(perm.isAllowEdit());
        dto.setAllowEditAll(perm.isAllowEditAll());
        dto.setAllowManageUsers(perm.isAllowManageUsers());
        dto.setAllowManageAllUsers(perm.isAllowManageAllUsers());
        dto.setPartner(new PartnerDTO(perm.getPartner().getId(), perm.getPartner().getName()));
        dto.setFolderLimitation(!Strings.isNullOrEmpty(perm.getModel()));
        dto.setFolders(folderList(folderMap, perm));
        models.add(dto);
    }
    return new UserResult(models, cmd.getOffset(), queryTotalCount(cmd, currentUser, whereClause));
}
Also used : PartnerDTO(org.activityinfo.legacy.shared.model.PartnerDTO) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Folder(org.activityinfo.server.database.hibernate.entity.Folder) UserPermissionDTO(org.activityinfo.legacy.shared.model.UserPermissionDTO) ResourceId(org.activityinfo.model.resource.ResourceId) Database(org.activityinfo.server.database.hibernate.entity.Database) UserResult(org.activityinfo.legacy.shared.command.result.UserResult) UserPermission(org.activityinfo.server.database.hibernate.entity.UserPermission)

Example 3 with UserPermission

use of org.activityinfo.server.database.hibernate.entity.UserPermission in project activityinfo by bedatadriven.

the class SignUpConfirmationController method addUserToDefaultDatabase.

protected void addUserToDefaultDatabase(User user) {
    Database database = entityManager.find(Database.class, DEFAULT_DATABASE_ID);
    if (database == null) {
        LOGGER.severe("Default database " + DEFAULT_DATABASE_ID + " does not exist, unable to add user " + user.getEmail());
        return;
    }
    Partner partner = entityManager.find(Partner.class, DEFAULT_PARTNER_ID);
    if (partner == null) {
        LOGGER.severe("Default partner " + DEFAULT_PARTNER_ID + " does not exist, unable to add user " + user.getEmail());
        return;
    }
    UserPermission permission = new UserPermission(database, user);
    permission.setPartner(partner);
    permission.setAllowView(true);
    permission.setAllowViewAll(true);
    permission.setLastSchemaUpdate(new Date());
    entityManager.persist(permission);
}
Also used : Database(org.activityinfo.server.database.hibernate.entity.Database) Partner(org.activityinfo.server.database.hibernate.entity.Partner) Date(java.util.Date) UserPermission(org.activityinfo.server.database.hibernate.entity.UserPermission)

Example 4 with UserPermission

use of org.activityinfo.server.database.hibernate.entity.UserPermission in project activityinfo by bedatadriven.

the class DesignAuthorizationHandler method isAuthorized.

@Override
public boolean isAuthorized(AuthenticatedUser requestingUser, SchemaElement entity) {
    Preconditions.checkNotNull(requestingUser, "requestingUser");
    Database database = entity.findOwningDatabase();
    if (database.getOwner().getId() == requestingUser.getId()) {
        return true;
    }
    for (UserPermission permission : database.getUserPermissions()) {
        if (permission.getUser().getId() == requestingUser.getId() && permission.isAllowDesign()) {
            return true;
        }
    }
    return false;
}
Also used : Database(org.activityinfo.server.database.hibernate.entity.Database) UserPermission(org.activityinfo.server.database.hibernate.entity.UserPermission)

Example 5 with UserPermission

use of org.activityinfo.server.database.hibernate.entity.UserPermission in project activityinfo by bedatadriven.

the class UpdateUserPermissionsHandler method execute.

@Override
public CommandResult execute(UpdateUserPermissions cmd, User executingUser) {
    LOGGER.info("UpdateUserPermissions: " + cmd);
    Database database = databaseDAO.findById(cmd.getDatabaseId());
    UserPermissionDTO dto = cmd.getModel();
    /*
         * First check that the current user has permission to add users to to
         * the queries
         */
    boolean isOwner = executingUser.getId() == database.getOwner().getId();
    UserPermission executingUserPermission = queryUserPermission(executingUser, database);
    LOGGER.info("executingUserPermission: isOwner: " + isOwner + ", executingUserPermissions: " + cmd);
    if (!isOwner) {
        verifyAuthority(cmd, executingUserPermission);
    }
    /* Database owner cannot be added */
    if (database.getOwner().getEmail().equalsIgnoreCase(cmd.getModel().getEmail())) {
        throw new UserExistsException();
    }
    User user = null;
    if (userDAO.doesUserExist(dto.getEmail())) {
        user = userDAO.findUserByEmail(dto.getEmail());
    }
    if (user == null) {
        user = createNewUser(executingUser, dto);
    }
    /*
         * Does the permission record exist ?
         */
    UserPermission perm = queryUserPermission(user, database);
    if (perm == null) {
        perm = new UserPermission(database, user);
        doUpdate(perm, dto, isOwner, executingUserPermission);
        permDAO.persist(perm);
    } else {
        // If the user is intending to add a new user, verify that this user doesn't already exist
        if (cmd.isNewUser() && perm.isAllowView()) {
            throw new UserExistsException();
        }
        doUpdate(perm, dto, isOwner, executingUserPermission);
    }
    return null;
}
Also used : UserExistsException(org.activityinfo.legacy.shared.command.result.UserExistsException) User(org.activityinfo.server.database.hibernate.entity.User) Database(org.activityinfo.server.database.hibernate.entity.Database) UserPermissionDTO(org.activityinfo.legacy.shared.model.UserPermissionDTO) UserPermission(org.activityinfo.server.database.hibernate.entity.UserPermission)

Aggregations

Database (org.activityinfo.server.database.hibernate.entity.Database)5 UserPermission (org.activityinfo.server.database.hibernate.entity.UserPermission)5 ArrayList (java.util.ArrayList)2 UserPermissionDTO (org.activityinfo.legacy.shared.model.UserPermissionDTO)2 Date (java.util.Date)1 HashMap (java.util.HashMap)1 GetSyncRegions (org.activityinfo.legacy.shared.command.GetSyncRegions)1 SyncRegion (org.activityinfo.legacy.shared.command.result.SyncRegion)1 SyncRegions (org.activityinfo.legacy.shared.command.result.SyncRegions)1 UserExistsException (org.activityinfo.legacy.shared.command.result.UserExistsException)1 UserResult (org.activityinfo.legacy.shared.command.result.UserResult)1 PartnerDTO (org.activityinfo.legacy.shared.model.PartnerDTO)1 ResourceId (org.activityinfo.model.resource.ResourceId)1 Folder (org.activityinfo.server.database.hibernate.entity.Folder)1 Partner (org.activityinfo.server.database.hibernate.entity.Partner)1 User (org.activityinfo.server.database.hibernate.entity.User)1