use of org.mx.comps.rbac.dal.entity.Privilege in project main by JohnPeng739.
the class RoleManageServiceImpl method save.
/**
* {@inheritDoc}
*
* @see RoleManageServiceCommonImpl#save(Role)
*/
@Override
public Role save(Role role) {
Set<Privilege> oldPrivileges = new HashSet<>();
Set<Account> oldAccounts = new HashSet<>();
if (!StringUtils.isBlank(role.getId())) {
Role checked = accessor.getById(role.getId(), Role.class);
oldPrivileges.addAll(checked.getPrivileges());
oldAccounts.addAll(checked.getAccounts());
}
accessor.save(role, false);
Set<Privilege> privileges = role.getPrivileges();
Set<Account> accounts = role.getAccounts();
for (Privilege privilege : privileges) {
if (oldPrivileges.contains(privilege)) {
oldPrivileges.remove(privilege);
continue;
} else {
privilege.getRoles().add(role);
accessor.save(privilege, false);
}
}
for (Privilege privilege : oldPrivileges) {
privilege.getRoles().remove(role);
accessor.save(privilege, false);
}
for (Account account : accounts) {
if (oldAccounts.contains(account)) {
oldAccounts.remove(account);
continue;
} else {
account.getRoles().add(role);
accessor.save(account, false);
}
}
for (Account account : oldAccounts) {
account.getRoles().remove(role);
accessor.save(account, false);
}
return role;
}
Aggregations