Search in sources :

Example 6 with UserDatabase

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);
}
Also used : UserDatabase(org.activityinfo.server.database.hibernate.entity.UserDatabase) Partner(org.activityinfo.server.database.hibernate.entity.Partner) Date(java.util.Date) UserPermission(org.activityinfo.server.database.hibernate.entity.UserPermission) LogException(org.activityinfo.server.util.logging.LogException)

Example 7 with UserDatabase

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);
}
Also used : MailSenderStub(org.activityinfo.server.mail.MailSenderStub) User(org.activityinfo.server.database.hibernate.entity.User) PartnerDTO(org.activityinfo.shared.dto.PartnerDTO) TemplateModule(org.activityinfo.server.util.TemplateModule) UpdateUserPermissionsHandler(org.activityinfo.server.command.handler.UpdateUserPermissionsHandler) UserDatabase(org.activityinfo.server.database.hibernate.entity.UserDatabase) Partner(org.activityinfo.server.database.hibernate.entity.Partner) Before(org.junit.Before)

Example 8 with UserDatabase

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());
}
Also used : Project(org.activityinfo.server.database.hibernate.entity.Project) CreateResult(org.activityinfo.shared.command.result.CreateResult) UserDatabase(org.activityinfo.server.database.hibernate.entity.UserDatabase) Activity(org.activityinfo.server.database.hibernate.entity.Activity) CommandException(org.activityinfo.shared.exception.CommandException) CreateLockedPeriod(org.activityinfo.shared.command.CreateLockedPeriod) LockedPeriod(org.activityinfo.server.database.hibernate.entity.LockedPeriod) Date(java.util.Date) LockedPeriodDTO(org.activityinfo.shared.dto.LockedPeriodDTO)

Example 9 with UserDatabase

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();
}
Also used : RemoveResult(org.activityinfo.shared.command.result.RemoveResult) IllegalAccessCommandException(org.activityinfo.shared.exception.IllegalAccessCommandException) RemoveFailedResult(org.activityinfo.shared.command.result.RemoveFailedResult) UserDatabase(org.activityinfo.server.database.hibernate.entity.UserDatabase) Partner(org.activityinfo.server.database.hibernate.entity.Partner) RemovePartner(org.activityinfo.shared.command.RemovePartner) Date(java.util.Date) UserPermission(org.activityinfo.server.database.hibernate.entity.UserPermission)

Example 10 with UserDatabase

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;
}
Also used : User(org.activityinfo.server.database.hibernate.entity.User) UserDatabase(org.activityinfo.server.database.hibernate.entity.UserDatabase) UserPermissionDTO(org.activityinfo.shared.dto.UserPermissionDTO) UserPermission(org.activityinfo.server.database.hibernate.entity.UserPermission)

Aggregations

UserDatabase (org.activityinfo.server.database.hibernate.entity.UserDatabase)14 Partner (org.activityinfo.server.database.hibernate.entity.Partner)7 Date (java.util.Date)6 UserPermission (org.activityinfo.server.database.hibernate.entity.UserPermission)5 Activity (org.activityinfo.server.database.hibernate.entity.Activity)4 Project (org.activityinfo.server.database.hibernate.entity.Project)4 CreateResult (org.activityinfo.shared.command.result.CreateResult)4 User (org.activityinfo.server.database.hibernate.entity.User)3 AttributeGroup (org.activityinfo.server.database.hibernate.entity.AttributeGroup)2 IllegalAccessCommandException (org.activityinfo.shared.exception.IllegalAccessCommandException)2 UpdateUserPermissionsHandler (org.activityinfo.server.command.handler.UpdateUserPermissionsHandler)1 AdminEntity (org.activityinfo.server.database.hibernate.entity.AdminEntity)1 Attribute (org.activityinfo.server.database.hibernate.entity.Attribute)1 Indicator (org.activityinfo.server.database.hibernate.entity.Indicator)1 IndicatorLinkEntity (org.activityinfo.server.database.hibernate.entity.IndicatorLinkEntity)1 LocationType (org.activityinfo.server.database.hibernate.entity.LocationType)1 LockedPeriod (org.activityinfo.server.database.hibernate.entity.LockedPeriod)1 Target (org.activityinfo.server.database.hibernate.entity.Target)1 MailSenderStub (org.activityinfo.server.mail.MailSenderStub)1 TemplateModule (org.activityinfo.server.util.TemplateModule)1