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);
}
}
}
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;
}
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;
}
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";
}
}
}
}
}
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;
}
Aggregations