Search in sources :

Example 1 with Permission

use of com.axelor.auth.db.Permission in project axelor-open-suite by axelor.

the class PermissionAssistantService method writePermission.

private int writePermission(MetaModel object, MetaField userField, String[] row, int colIndex, String permName) {
    Permission perm = permissionRepository.findByName(permName);
    if (perm != null && perm.getObject().equals(object.getFullName())) {
        row[colIndex++] = !perm.getCanRead() ? "" : "x";
        row[colIndex++] = !perm.getCanWrite() ? "" : "x";
        row[colIndex++] = !perm.getCanCreate() ? "" : "x";
        row[colIndex++] = !perm.getCanRemove() ? "" : "x";
        row[colIndex++] = !perm.getCanExport() ? "" : "x";
        row[colIndex++] = Strings.isNullOrEmpty(perm.getCondition()) ? "" : perm.getCondition();
        row[colIndex++] = Strings.isNullOrEmpty(perm.getConditionParams()) ? "" : perm.getConditionParams();
        // readonly if
        row[colIndex++] = "";
        // hide if
        row[colIndex++] = "";
    } else if (userField != null) {
        MetaField objectField = fieldRepository.all().filter("self.typeName = ?1 and self.metaModel = ?2 and self.relationship = 'ManyToOne'", userField.getTypeName(), object).fetchOne();
        if (objectField != null) {
            String condition = "";
            String conditionParams = "__user__." + userField.getName();
            if (userField.getRelationship().contentEquals("ManyToOne")) {
                condition = "self." + objectField.getName() + " = ?";
            } else {
                condition = "self." + objectField.getName() + " in (?)";
            }
            row[colIndex++] = "x";
            row[colIndex++] = "x";
            row[colIndex++] = "x";
            row[colIndex++] = "x";
            row[colIndex++] = "x";
            row[colIndex++] = condition;
            row[colIndex++] = conditionParams;
            // readonly if
            row[colIndex++] = "";
            // hide if
            row[colIndex++] = "";
        }
    }
    return colIndex;
}
Also used : MetaField(com.axelor.meta.db.MetaField) MetaPermission(com.axelor.meta.db.MetaPermission) Permission(com.axelor.auth.db.Permission)

Example 2 with Permission

use of com.axelor.auth.db.Permission in project axelor-open-suite by axelor.

the class PermissionAssistantService method updatePermission.

public void updatePermission(Group group, String objectName, MetaField field, String[] row) {
    String[] objectNames = objectName.split("\\.");
    String permName = getPermissionName(field, objectNames[objectNames.length - 1], group.getCode());
    Permission permission = permissionRepository.all().filter("self.name = ?1", permName).fetchOne();
    boolean newPermission = false;
    if (permission == null) {
        newPermission = true;
        permission = new Permission();
        permission.setName(permName);
        permission.setObject(objectName);
    }
    permission.setCanRead(row[0].equalsIgnoreCase("x"));
    permission.setCanWrite(row[1].equalsIgnoreCase("x"));
    permission.setCanCreate(row[2].equalsIgnoreCase("x"));
    permission.setCanRemove(row[3].equalsIgnoreCase("x"));
    permission.setCanExport(row[4].equalsIgnoreCase("x"));
    if (newPermission) {
        group.addPermission(permission);
    }
}
Also used : MetaPermission(com.axelor.meta.db.MetaPermission) Permission(com.axelor.auth.db.Permission)

Example 3 with Permission

use of com.axelor.auth.db.Permission in project axelor-open-suite by axelor.

the class AccessConfigImportServiceImpl method getPermission.

@Transactional
public Permission getPermission(String model, String value, AccessConfig config) {
    String[] objs = model.split("\\.");
    String obj = objs[objs.length - 1];
    String pkg = objs[objs.length - 3];
    String name = "perm." + pkg + "." + obj + "." + value;
    Permission permission = permissionRepo.all().filter("self.name = ?1 and self.object = ?2", name, model).fetchOne();
    if (permission == null) {
        permission = new Permission(name);
        permission.setObject(model);
    }
    boolean defaultRight = value.equals("all");
    permission.setCanCreate(defaultRight);
    permission.setCanRead(defaultRight);
    permission.setCanWrite(defaultRight);
    permission.setCanRemove(defaultRight);
    permission.setCanExport(defaultRight);
    if (!defaultRight) {
        for (char c : value.toCharArray()) {
            switch(c) {
                case 'r':
                    {
                        permission.setCanRead(true);
                        break;
                    }
                case 'c':
                    {
                        permission.setCanCreate(true);
                        break;
                    }
                case 'w':
                    {
                        permission.setCanWrite(true);
                        break;
                    }
                case 'd':
                    {
                        permission.setCanRemove(true);
                        break;
                    }
                case 'e':
                    {
                        permission.setCanExport(true);
                        break;
                    }
            }
        }
    }
    return permissionRepo.save(permission);
}
Also used : Permission(com.axelor.auth.db.Permission) Transactional(com.google.inject.persist.Transactional)

Example 4 with Permission

use of com.axelor.auth.db.Permission in project axelor-open-suite by axelor.

the class ImportPermission method importPermission.

@Transactional
public Object importPermission(Object bean, Map<String, Object> values) {
    assert bean instanceof Permission;
    try {
        GroupRepository groupRepository = Beans.get(GroupRepository.class);
        Permission permission = (Permission) bean;
        String groups = (String) values.get("group");
        if (permission.getId() != null) {
            if (groups != null && !groups.isEmpty()) {
                for (Group group : groupRepository.all().filter("code in ?1", Arrays.asList(groups.split("\\|"))).fetch()) {
                    Set<Permission> permissions = group.getPermissions();
                    if (permissions == null)
                        permissions = new HashSet<Permission>();
                    permissions.add(permissionRepo.find(permission.getId()));
                    group.setPermissions(permissions);
                    groupRepository.save(group);
                }
            }
        }
        return permission;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return bean;
}
Also used : Group(com.axelor.auth.db.Group) Permission(com.axelor.auth.db.Permission) GroupRepository(com.axelor.auth.db.repo.GroupRepository) HashSet(java.util.HashSet) Transactional(com.google.inject.persist.Transactional)

Example 5 with Permission

use of com.axelor.auth.db.Permission in project axelor-open-suite by axelor.

the class ImportPermission method importPermissionToRole.

@Transactional
public Object importPermissionToRole(Object bean, Map<String, Object> values) {
    assert bean instanceof Permission;
    Permission permission = (Permission) bean;
    String roleName = values.get("roleName").toString();
    if (Strings.isNullOrEmpty(roleName)) {
        return bean;
    }
    RoleRepository roleRepository = Beans.get(RoleRepository.class);
    Role role = roleRepository.findByName(roleName);
    if (role == null) {
        return bean;
    }
    role.addPermission(permission);
    return bean;
}
Also used : Role(com.axelor.auth.db.Role) Permission(com.axelor.auth.db.Permission) RoleRepository(com.axelor.auth.db.repo.RoleRepository) Transactional(com.google.inject.persist.Transactional)

Aggregations

Permission (com.axelor.auth.db.Permission)7 MetaPermission (com.axelor.meta.db.MetaPermission)3 Transactional (com.google.inject.persist.Transactional)3 AccessConfig (com.axelor.apps.base.db.AccessConfig)1 Group (com.axelor.auth.db.Group)1 Role (com.axelor.auth.db.Role)1 GroupRepository (com.axelor.auth.db.repo.GroupRepository)1 RoleRepository (com.axelor.auth.db.repo.RoleRepository)1 MetaField (com.axelor.meta.db.MetaField)1 HashSet (java.util.HashSet)1 Cell (org.apache.poi.ss.usermodel.Cell)1