use of org.activityinfo.server.database.hibernate.entity.UserDatabase in project activityinfo by bedatadriven.
the class SignUpConfirmationController method addUserToDefaultDatabase.
@LogException(emailAlert = true)
protected void addUserToDefaultDatabase(User user) {
UserDatabase database = databaseDAO.get().findById(DEFAULT_DATABASE_ID);
Partner partner = partnerDAO.get().findById(DEFAULT_PARTNER_ID);
UserPermission permission = new UserPermission(database, user);
permission.setPartner(partner);
permission.setAllowView(true);
permission.setAllowViewAll(true);
permission.setLastSchemaUpdate(new Date());
permissionDAO.get().persist(permission);
}
use of org.activityinfo.server.database.hibernate.entity.UserDatabase in project activityinfo by bedatadriven.
the class UpdateUserPermissionsHandlerTest method setup.
@Before
public void setup() throws TemplateModelException {
NRC = new Partner();
NRC.setId(1);
NRC.setName("NRC");
NRC.setFullName("Norwegian Refugee Council");
db.persist(NRC);
IRC = new Partner();
IRC.setId(2);
IRC.setName("IRC");
IRC.setFullName("International Rescue Committee");
db.persist(IRC);
NRC_DTO = new PartnerDTO(1, "NRC");
TemplateModule templateModule = new TemplateModule();
mailer = new MailSenderStub(templateModule.provideConfiguration(Providers.of(Domain.DEFAULT)));
handler = new UpdateUserPermissionsHandler(db.getDAO(UserDatabaseDAO.class), db.getDAO(PartnerDAO.class), db.getDAO(UserDAO.class), db.getDAO(UserPermissionDAO.class), mailer);
owner = new User();
owner.setId(99);
owner.setName("Alex");
owner.setEmail("alex@bedatadriven.com");
db.persist(owner);
UserDatabase udb = new UserDatabase(1, "PEAR");
udb.setOwner(owner);
db.persist(udb);
}
use of org.activityinfo.server.database.hibernate.entity.UserDatabase in project activityinfo by bedatadriven.
the class CreateLockedPeriodHandler method execute.
@Override
public CommandResult execute(CreateLockedPeriod cmd, User user) throws CommandException {
Activity activity = null;
UserDatabase database = null;
Project project = null;
LockedPeriod lockedPeriod = new LockedPeriod();
LockedPeriodDTO lockedPeriodDTO = cmd.getLockedPeriod();
lockedPeriod.setFromDate(lockedPeriodDTO.getFromDate().atMidnightInMyTimezone());
lockedPeriod.setToDate(lockedPeriodDTO.getToDate().atMidnightInMyTimezone());
lockedPeriod.setName(lockedPeriodDTO.getName());
lockedPeriod.setEnabled(lockedPeriodDTO.isEnabled());
int databaseId;
if (cmd.getUserDatabseId() != 0) {
database = em.find(UserDatabase.class, cmd.getUserDatabseId());
lockedPeriod.setUserDatabase(database);
databaseId = database.getId();
} else if (cmd.getProjectId() != 0) {
project = em.find(Project.class, cmd.getProjectId());
lockedPeriod.setProject(project);
databaseId = project.getUserDatabase().getId();
} else if (cmd.getActivityId() != 0) {
activity = em.find(Activity.class, cmd.getActivityId());
lockedPeriod.setActivity(activity);
databaseId = activity.getDatabase().getId();
} else {
throw new CommandException("One of the following must be provdied: userDatabaseId, projectId, activityId");
}
UserDatabase db = em.find(UserDatabase.class, databaseId);
em.persist(lockedPeriod);
db.setLastSchemaUpdate(new Date());
em.persist(db);
if (database != null) {
database.getLockedPeriods().add(lockedPeriod);
}
if (project != null) {
project.getLockedPeriods().add(lockedPeriod);
}
if (activity != null) {
activity.getLockedPeriods().add(lockedPeriod);
}
return new CreateResult(lockedPeriod.getId());
}
use of org.activityinfo.server.database.hibernate.entity.UserDatabase in project activityinfo by bedatadriven.
the class RemovePartnerHandler method execute.
@Override
public CommandResult execute(RemovePartner cmd, User user) throws CommandException {
// verify the current user has access to this site
UserDatabase db = em.find(UserDatabase.class, cmd.getDatabaseId());
if (db.getOwner().getId() != user.getId()) {
UserPermission perm = db.getPermissionByUser(user);
if (perm == null || !perm.isAllowDesign()) {
throw new IllegalAccessCommandException();
}
}
// check to see if there are already sites associated with this partner
int siteCount = ((Number) em.createQuery("select count(s) " + "from Site s " + "where s.activity.id in (select a.id from Activity a where a.database.id = :dbId) " + "and s.partner.id = :partnerId " + "and s.dateDeleted is null").setParameter("dbId", cmd.getDatabaseId()).setParameter("partnerId", cmd.getPartnerId()).getSingleResult()).intValue();
if (siteCount > 0) {
return new RemoveFailedResult();
}
db.getPartners().remove(em.getReference(Partner.class, cmd.getPartnerId()));
db.setLastSchemaUpdate(new Date());
return new RemoveResult();
}
use of org.activityinfo.server.database.hibernate.entity.UserDatabase in project activityinfo by bedatadriven.
the class UpdateUserPermissionsHandler method execute.
@Override
public CommandResult execute(UpdateUserPermissions cmd, User executingUser) throws CommandException {
UserDatabase 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();
if (!isOwner) {
verifyAuthority(cmd, database.getPermissionByUser(executingUser));
}
User user = null;
if (userDAO.doesUserExist(dto.getEmail())) {
user = userDAO.findUserByEmail(dto.getEmail());
}
if (user == null) {
user = createNewUser(executingUser, dto, cmd.getHost());
}
/*
* Does the permission record exist ?
*/
UserPermission perm = database.getPermissionByUser(user);
if (perm == null) {
perm = new UserPermission(database, user);
doUpdate(perm, dto, isOwner, database.getPermissionByUser(executingUser));
permDAO.persist(perm);
} else {
doUpdate(perm, dto, isOwner, database.getPermissionByUser(executingUser));
}
return null;
}
Aggregations