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);
}
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));
}
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);
}
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;
}
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;
}
Aggregations