/** * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at http://mozilla.org/MPL/2.0/. */ package org.mifosplatform.useradministration.domain; import java.util.Collection; import java.util.HashSet; import java.util.LinkedHashMap; import java.util.Map; import java.util.Set; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.JoinColumn; import javax.persistence.JoinTable; import javax.persistence.ManyToMany; import javax.persistence.Table; import javax.persistence.UniqueConstraint; import org.mifosplatform.infrastructure.core.api.JsonCommand; import org.mifosplatform.useradministration.data.RoleData; import org.springframework.data.jpa.domain.AbstractPersistable; @Entity @Table(name = "m_role", uniqueConstraints = { @UniqueConstraint(columnNames = { "name" }, name = "unq_name") }) public class Role extends AbstractPersistable<Long> { @Column(name = "name", unique = true, nullable = false, length = 100) private String name; @Column(name = "description", nullable = false, length = 500) private String description; @Column(name = "is_disabled", nullable = false) private Boolean disabled; @ManyToMany(fetch = FetchType.EAGER) @JoinTable(name = "m_role_permission", joinColumns = @JoinColumn(name = "role_id"), inverseJoinColumns = @JoinColumn(name = "permission_id")) private final Set<Permission> permissions = new HashSet<>(); public static Role fromJson(final JsonCommand command) { final String name = command.stringValueOfParameterNamed("name"); final String description = command.stringValueOfParameterNamed("description"); return new Role(name, description); } protected Role() { // } public Role(final String name, final String description) { this.name = name.trim(); this.description = description.trim(); this.disabled = false; } public Map<String, Object> update(final JsonCommand command) { final Map<String, Object> actualChanges = new LinkedHashMap<>(7); final String nameParamName = "name"; if (command.isChangeInStringParameterNamed(nameParamName, this.name)) { final String newValue = command.stringValueOfParameterNamed(nameParamName); actualChanges.put(nameParamName, newValue); this.name = newValue; } final String descriptionParamName = "description"; if (command.isChangeInStringParameterNamed(descriptionParamName, this.description)) { final String newValue = command.stringValueOfParameterNamed(descriptionParamName); actualChanges.put(descriptionParamName, newValue); this.description = newValue; } return actualChanges; } public boolean updatePermission(final Permission permission, final boolean isSelected) { boolean changed = false; if (isSelected) { changed = addPermission(permission); } else { changed = removePermission(permission); } return changed; } private boolean addPermission(final Permission permission) { return this.permissions.add(permission); } private boolean removePermission(final Permission permission) { return this.permissions.remove(permission); } public Collection<Permission> getPermissions() { return this.permissions; } public boolean hasPermissionTo(final String permissionCode) { boolean match = false; for (final Permission permission : this.permissions) { if (permission.hasCode(permissionCode)) { match = true; break; } } return match; } public RoleData toData() { return new RoleData(getId(), this.name, this.description, this.disabled); } public void disableRole() { this.disabled = true; } public Boolean isDisabled() { return this.disabled; } public void enableRole() { this.disabled = false; } public Boolean isEnabled() { return this.disabled; } }