Search in sources :

Example 6 with Group

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

the class MetaGroupMenuAssistantService method importMenus.

private void importMenus(String[] row, String[] groupRow, Map<String, Object> groupMap, Group admin) {
    List<MetaMenu> menus = menuRepository.all().filter("self.name = ?1", row[0]).order("-priority").fetch();
    if (menus.isEmpty()) {
        errorLog += "\n" + String.format(I18n.get(IMessage.NO_MENU), row[0]);
        return;
    }
    for (MetaMenu menu : menus) {
        boolean noAccess = true;
        for (Integer mIndex = 2; mIndex < row.length; mIndex++) {
            String code = groupRow[mIndex];
            Object object = groupMap.get(code);
            Role role = null;
            Group group = null;
            if (object instanceof Group) {
                group = (Group) object;
            } else if (object instanceof Role) {
                role = (Role) object;
            }
            if (row[mIndex].equalsIgnoreCase("x")) {
                noAccess = false;
                if (group != null) {
                    menu.addGroup(group);
                }
                if (role != null) {
                    menu.addRole(role);
                }
                updatedMenus.add(menu);
            } else if (group != null && menu.getGroups().contains(group)) {
                menu.removeGroup(group);
                updatedMenus.add(menu);
            } else if (role != null && menu.getRoles().contains(role)) {
                menu.removeRole(role);
                updatedMenus.add(menu);
            }
        }
        if (noAccess && admin != null) {
            menu.addGroup(admin);
        }
    }
}
Also used : Role(com.axelor.auth.db.Role) Group(com.axelor.auth.db.Group) MetaMenu(com.axelor.meta.db.MetaMenu)

Example 7 with Group

use of com.axelor.auth.db.Group 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 8 with Group

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

the class PermissionAssistantService method checkBadGroups.

private Map<String, Group> checkBadGroups(String[] groupRow) {
    List<String> badGroups = new ArrayList<>();
    Map<String, Group> groupMap = new HashMap<>();
    for (Integer glen = header.size() + 1; glen < groupRow.length; glen += groupHeader.size()) {
        String groupName = groupRow[glen];
        Group group = groupRepository.all().filter("self.code = ?1", groupName).fetchOne();
        if (group == null) {
            badGroups.add(groupName);
        } else {
            groupMap.put(groupName, group);
        }
    }
    if (!badGroups.isEmpty()) {
        errorLog += "\n" + String.format(I18n.get(IMessage.NO_GROUP), badGroups);
    }
    return groupMap;
}
Also used : Group(com.axelor.auth.db.Group) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList)

Example 9 with Group

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

the class MetaGroupMenuAssistantService method addGroupAccess.

private void addGroupAccess(List<String[]> rows) {
    ListIterator<String[]> rowIter = rows.listIterator();
    String[] header = rowIter.next();
    while (rowIter.hasNext()) {
        String[] row = rowIter.next();
        MetaMenu menu = menuRepository.all().filter("self.name = ?1", row[0]).order("-priority").fetchOne();
        if (row.length < header.length) {
            row = Arrays.copyOf(row, header.length);
            rowIter.set(row);
        }
        for (int i = 2; i < header.length; i++) {
            for (Group group : menu.getGroups()) {
                if (header[i] != null && header[i].equals(group.getCode())) {
                    row[i] = "x";
                }
            }
            for (Role role : menu.getRoles()) {
                if (header[i] != null && header[i].equals(role.getName())) {
                    row[i] = "x";
                }
            }
        }
    }
}
Also used : Role(com.axelor.auth.db.Role) Group(com.axelor.auth.db.Group) MetaMenu(com.axelor.meta.db.MetaMenu)

Example 10 with Group

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

the class MetaGroupMenuAssistantService method importGroupMenu.

public String importGroupMenu(MetaGroupMenuAssistant groupMenuAssistant) {
    try (CSVReader csvReader = new CSVReader(new InputStreamReader(new FileInputStream(MetaFiles.getPath(groupMenuAssistant.getMetaFile()).toFile()), StandardCharsets.UTF_8), ';')) {
        setBundle(new Locale(groupMenuAssistant.getLanguage()));
        String[] groupRow = csvReader.readNext();
        if (groupRow == null || groupRow.length < 3) {
            return I18n.get(IMessage.BAD_FILE);
        }
        Map<String, Object> groupMap = checkGroups(groupRow);
        groupMap.putAll(checkRoles(groupRow));
        badGroups.removeAll(groupMap.keySet());
        badRoles.removeAll(groupMap.keySet());
        if (!badGroups.isEmpty()) {
            errorLog += "\n" + String.format(I18n.get(IMessage.NO_GROUP), badGroups);
        }
        if (!badRoles.isEmpty()) {
            errorLog += "\n" + String.format(I18n.get(IMessage.NO_ROLE), badRoles);
        }
        Group admin = groupRepository.findByCode("admins");
        for (String[] row : csvReader.readAll()) {
            importMenus(row, groupRow, groupMap, admin);
        }
        saveMenus();
    } catch (Exception e) {
        TraceBackService.trace(e);
        errorLog += "\n" + String.format(I18n.get(IMessage.ERR_IMPORT_WITH_MSG), e.getMessage());
    }
    return errorLog;
}
Also used : Locale(java.util.Locale) Group(com.axelor.auth.db.Group) InputStreamReader(java.io.InputStreamReader) CSVReader(com.opencsv.CSVReader) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException)

Aggregations

Group (com.axelor.auth.db.Group)13 Role (com.axelor.auth.db.Role)8 MetaMenu (com.axelor.meta.db.MetaMenu)5 Transactional (com.google.inject.persist.Transactional)3 ArrayList (java.util.ArrayList)3 Locale (java.util.Locale)3 User (com.axelor.auth.db.User)2 GroupRepository (com.axelor.auth.db.repo.GroupRepository)2 RoleRepository (com.axelor.auth.db.repo.RoleRepository)2 MetaAction (com.axelor.meta.db.MetaAction)2 MetaMenuRepository (com.axelor.meta.db.repo.MetaMenuRepository)2 CSVReader (com.opencsv.CSVReader)2 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 InputStreamReader (java.io.InputStreamReader)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 ResourceBundle (java.util.ResourceBundle)2 ConfiguratorCreator (com.axelor.apps.sale.db.ConfiguratorCreator)1 Permission (com.axelor.auth.db.Permission)1