Search in sources :

Example 1 with Permission

use of fi.otavanopisto.pyramus.domainmodel.security.Permission in project pyramus by otavanopisto.

the class ImportExportPermissionsViewController method processSend.

@Override
public void processSend(PageRequestContext requestContext) {
    String permissionJson = requestContext.getString("permissionJson");
    JSONObject permissionMap = JSONObject.fromObject(permissionJson);
    PermissionDAO permissionDAO = DAOFactory.getInstance().getPermissionDAO();
    EnvironmentRolePermissionDAO environmentRolePermissionDAO = DAOFactory.getInstance().getEnvironmentRolePermissionDAO();
    List<EnvironmentRolePermission> allPermissions = environmentRolePermissionDAO.listAll();
    for (EnvironmentRolePermission erp : allPermissions) {
        environmentRolePermissionDAO.delete(erp);
    }
    for (Object roleObject : permissionMap.keySet()) {
        String roleValue = (String) roleObject;
        Role role = Role.getRole(Integer.valueOf(roleValue));
        if (role == null) {
            Logger.getLogger(getClass().getName()).severe(String.format("Role with value %s not found from system", roleValue));
            continue;
        }
        for (Object permissionNameObject : permissionMap.getJSONArray(roleValue)) {
            String permissionName = (String) permissionNameObject;
            Permission permission = permissionDAO.findByName(permissionName);
            if (permission == null) {
                Logger.getLogger(getClass().getName()).severe(String.format("Permission %s not found from system", permissionName));
                continue;
            }
            environmentRolePermissionDAO.create(role, permission);
        }
    }
    requestContext.setRedirectURL("/system/managepermissions.page");
}
Also used : Role(fi.otavanopisto.pyramus.domainmodel.users.Role) UserRole(fi.otavanopisto.pyramus.framework.UserRole) JSONObject(net.sf.json.JSONObject) EnvironmentRolePermissionDAO(fi.otavanopisto.pyramus.dao.security.EnvironmentRolePermissionDAO) PermissionDAO(fi.otavanopisto.pyramus.dao.security.PermissionDAO) EnvironmentRolePermissionDAO(fi.otavanopisto.pyramus.dao.security.EnvironmentRolePermissionDAO) EnvironmentRolePermission(fi.otavanopisto.pyramus.domainmodel.security.EnvironmentRolePermission) Permission(fi.otavanopisto.pyramus.domainmodel.security.Permission) EnvironmentRolePermission(fi.otavanopisto.pyramus.domainmodel.security.EnvironmentRolePermission) JSONObject(net.sf.json.JSONObject)

Example 2 with Permission

use of fi.otavanopisto.pyramus.domainmodel.security.Permission in project pyramus by otavanopisto.

the class ManagePermissionsViewController method processSend.

@Override
public void processSend(PageRequestContext requestContext) {
    String resetRole = requestContext.getString("roleReset");
    if (StringUtils.isBlank(resetRole)) {
        PermissionDAO permissionDAO = DAOFactory.getInstance().getPermissionDAO();
        EnvironmentRolePermissionDAO environmentRolePermissionDAO = DAOFactory.getInstance().getEnvironmentRolePermissionDAO();
        List<Permission> permissions = permissionDAO.listAll();
        for (Permission permission : permissions) {
            for (Role role : manageableRoles()) {
                String paramName = permission.getId().toString() + '.' + role.name();
                EnvironmentRolePermission rolePermission = environmentRolePermissionDAO.findByUserRoleAndPermission(role, permission);
                boolean isSet = new Integer(1).equals(requestContext.getInteger(paramName));
                boolean exists = rolePermission != null;
                if (isSet != exists) {
                    if (isSet) {
                        environmentRolePermissionDAO.create(role, permission);
                    } else {
                        environmentRolePermissionDAO.delete(rolePermission);
                    }
                }
            }
        }
    } else {
        resetRoles(Role.valueOf(resetRole));
    }
    processForm(requestContext);
}
Also used : Role(fi.otavanopisto.pyramus.domainmodel.users.Role) UserRole(fi.otavanopisto.pyramus.framework.UserRole) EnvironmentRolePermissionDAO(fi.otavanopisto.pyramus.dao.security.EnvironmentRolePermissionDAO) PermissionDAO(fi.otavanopisto.pyramus.dao.security.PermissionDAO) EnvironmentRolePermissionDAO(fi.otavanopisto.pyramus.dao.security.EnvironmentRolePermissionDAO) EnvironmentRolePermission(fi.otavanopisto.pyramus.domainmodel.security.EnvironmentRolePermission) Permission(fi.otavanopisto.pyramus.domainmodel.security.Permission) EnvironmentRolePermission(fi.otavanopisto.pyramus.domainmodel.security.EnvironmentRolePermission)

Example 3 with Permission

use of fi.otavanopisto.pyramus.domainmodel.security.Permission in project pyramus by otavanopisto.

the class PermissionDAO method listByScope.

public List<Permission> listByScope(String scope) {
    EntityManager entityManager = getEntityManager();
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Permission> criteria = criteriaBuilder.createQuery(Permission.class);
    Root<Permission> root = criteria.from(Permission.class);
    criteria.select(root);
    criteria.where(criteriaBuilder.equal(root.get(Permission_.scope), scope));
    return entityManager.createQuery(criteria).getResultList();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) Permission(fi.otavanopisto.pyramus.domainmodel.security.Permission)

Example 4 with Permission

use of fi.otavanopisto.pyramus.domainmodel.security.Permission in project pyramus by otavanopisto.

the class PermissionDAO method findByName.

public Permission findByName(String name) {
    EntityManager entityManager = getEntityManager();
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Permission> criteria = criteriaBuilder.createQuery(Permission.class);
    Root<Permission> root = criteria.from(Permission.class);
    criteria.select(root);
    criteria.where(criteriaBuilder.equal(root.get(Permission_.name), name));
    return getSingleResult(entityManager.createQuery(criteria));
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) Permission(fi.otavanopisto.pyramus.domainmodel.security.Permission)

Example 5 with Permission

use of fi.otavanopisto.pyramus.domainmodel.security.Permission in project pyramus by otavanopisto.

the class PermissionDAO method create.

public Permission create(String name, String scope) {
    Permission permission = new Permission();
    permission.setName(name);
    permission.setScope(scope);
    getEntityManager().persist(permission);
    return permission;
}
Also used : Permission(fi.otavanopisto.pyramus.domainmodel.security.Permission)

Aggregations

Permission (fi.otavanopisto.pyramus.domainmodel.security.Permission)9 EnvironmentRolePermission (fi.otavanopisto.pyramus.domainmodel.security.EnvironmentRolePermission)5 Role (fi.otavanopisto.pyramus.domainmodel.users.Role)4 EnvironmentRolePermissionDAO (fi.otavanopisto.pyramus.dao.security.EnvironmentRolePermissionDAO)3 PermissionDAO (fi.otavanopisto.pyramus.dao.security.PermissionDAO)3 UserRole (fi.otavanopisto.pyramus.framework.UserRole)2 EntityManager (javax.persistence.EntityManager)2 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)2 HashMap (java.util.HashMap)1 PostConstruct (javax.annotation.PostConstruct)1 JSONObject (net.sf.json.JSONObject)1