Skip to content

Commit

Permalink
[kbss-cvut/record-manager-ui#202] Implement Roles and permission
Browse files Browse the repository at this point in the history
  • Loading branch information
palagdan committed Sep 17, 2024
1 parent 0d96711 commit 5c84bf4
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 136 deletions.
106 changes: 106 additions & 0 deletions src/main/java/cz/cvut/kbss/study/model/Permission.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package cz.cvut.kbss.study.model;


import cz.cvut.kbss.jopa.model.annotations.Individual;
import lombok.Getter;

@Getter
public enum Permission {

@Individual(iri=Vocabulary.s_i_delete_all_records_permission)
deleteAllRecords(Vocabulary.s_i_delete_all_records_permission),

@Individual(iri=Vocabulary.s_i_view_all_records_permission)
viewAllRecords(Vocabulary.s_i_view_all_records_permission),

@Individual(iri=Vocabulary.s_i_edit_all_records_permission)
editAllRecords(Vocabulary.s_i_edit_all_records_permission),

@Individual(iri=Vocabulary.s_i_delete_organization_records_permission)
deleteOrganizationRecords(Vocabulary.s_i_delete_organization_records_permission),

@Individual(iri=Vocabulary.s_i_view_organization_records_permission)
viewOrganizationRecords(Vocabulary.s_i_view_organization_records_permission),

@Individual(iri=Vocabulary.s_i_edit_organization_records_permission)
editOrganizationRecords(Vocabulary.s_i_edit_organization_records_permission),

@Individual(iri=Vocabulary.s_i_edit_users_permission)
editUsers(Vocabulary.s_i_edit_users_permission),

@Individual(iri=Vocabulary.s_i_complete_records_permission)
completeRecords(Vocabulary.s_i_complete_records_permission),

@Individual(iri=Vocabulary.s_i_reject_records_permission)
rejectRecords(Vocabulary.s_i_reject_records_permission),

@Individual(iri=Vocabulary.s_i_publish_records_permission)
publishRecords(Vocabulary.s_i_publish_records_permission),

@Individual(iri=Vocabulary.s_i_import_codelists_permission)
importCodelists(Vocabulary.s_i_import_codelists_permission);

private final String iri;


Permission(String iri) {
this.iri = iri;
}

public String getIri() {
return iri;
}


/**
* Returns {@link Permission} with the specified IRI.
*
* @param iri permission identifier
* @return matching {@code Permission}
* @throws IllegalArgumentException When no matching permission is found
*/
public static Permission fromIri(String iri) {
for (Permission p : values()) {
if (p.getIri().equals(iri)) {
return p;
}
}
throw new IllegalArgumentException("Unknown permission identifier '" + iri + "'.");
}


/**
* Returns {@link Permission} with the specified constant name.
*
* @param name permission name
* @return matching {@code Permission}
* @throws IllegalArgumentException When no matching permission is found
*/
public static Permission fromName(String name) {
for (Permission p : values()) {
if (p.name().equalsIgnoreCase(name)) {
return p;
}
}
throw new IllegalArgumentException("Unknown permission '" + name + "'.");
}


/**
* Returns a {@link Permission} with the specified IRI or constant name.
* <p>
* This function first tries to find the enum constant by IRI. If it is not found, constant name matching is
* attempted.
*
* @param identification Constant IRI or name to find match by
* @return matching {@code Permission}
* @throws IllegalArgumentException When no matching permission is found
*/
public static Permission fromIriOrName(String identification) {
try {
return fromIri(identification);
} catch (IllegalArgumentException e) {
return fromName(identification);
}
}
}
26 changes: 26 additions & 0 deletions src/main/java/cz/cvut/kbss/study/model/Role.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package cz.cvut.kbss.study.model;

import cz.cvut.kbss.jopa.model.annotations.Id;
import cz.cvut.kbss.jopa.model.annotations.OWLClass;
import cz.cvut.kbss.jopa.model.annotations.OWLDataProperty;
import cz.cvut.kbss.jopa.model.annotations.OWLObjectProperty;
import lombok.Getter;
import lombok.Setter;

import java.net.URI;
import java.util.Set;

@OWLClass(iri = Vocabulary.s_c_role)
@Getter
@Setter
public class Role {

@Id
private URI uri;

@OWLDataProperty(iri = Vocabulary.s_p_roleName)
private String roleName;

@OWLObjectProperty(iri = Vocabulary.s_p_has_permission)
private Set<Permission> permission;
}
54 changes: 20 additions & 34 deletions src/main/java/cz/cvut/kbss/study/model/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,23 @@
import cz.cvut.kbss.jopa.model.annotations.OWLDataProperty;
import cz.cvut.kbss.jopa.model.annotations.OWLObjectProperty;
import cz.cvut.kbss.jopa.model.annotations.ParticipationConstraints;
import cz.cvut.kbss.jopa.model.annotations.Types;
import cz.cvut.kbss.study.model.util.HasDerivableUri;
import cz.cvut.kbss.study.util.Constants;
import cz.cvut.kbss.study.util.IdentificationUtils;
import lombok.Getter;
import lombok.Setter;
import org.springframework.security.crypto.password.PasswordEncoder;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;


@OWLClass(iri = Vocabulary.s_c_Person)
@Getter
@Setter
public class User implements HasDerivableUri, Serializable {

@Id
Expand Down Expand Up @@ -61,23 +62,12 @@ public class User implements HasDerivableUri, Serializable {
@OWLObjectProperty(iri = Vocabulary.s_p_is_member_of, fetch = FetchType.EAGER)
private Institution institution;

@OWLObjectProperty(iri = Vocabulary.s_p_has_role_group)
private String roleGroup;

@OWLObjectProperty(iri = Vocabulary.s_p_has_role)
private Set<String> types;
private Role role;

public String getRoleGroup() {
return roleGroup;
}

public void setRoleGroup(String roleGroup) {
this.roleGroup = roleGroup;
}

public User() {
this.types = new HashSet<>();
types.add(Vocabulary.s_i_user);

}

@Override
Expand Down Expand Up @@ -148,18 +138,6 @@ public void setInstitution(Institution institution) {
this.institution = institution;
}

public Set<String> getTypes() {
return types;
}

public void setTypes(Set<String> types) {
this.types = types;
}

public void addType(String type) {
assert types != null;
getTypes().add(type);
}

/**
* Returns true if this user is an admin.
Expand All @@ -168,10 +146,10 @@ public void addType(String type) {
*
* @return {@code true} if this is admin, {@code false} otherwise
*/
public boolean isAdmin() {
assert types != null;
return getTypes().contains(Vocabulary.s_i_administrator);
}
// public boolean isAdmin() {
// assert types != null;
// return getTypes().contains(Vocabulary.s_i_administrator);
// }

public String getToken() {
return token;
Expand All @@ -189,6 +167,14 @@ public void setIsInvited(Boolean isInvited) {
this.isInvited = isInvited;
}

public void setRole(Role role){
this.role = role;
}

public Role getRole(){
return role;
}

/**
* Encodes password of this person.
*
Expand Down Expand Up @@ -227,7 +213,7 @@ public User copy() {
copy.setInstitution(institution);
copy.setIsInvited(isInvited);
copy.setToken(token);
copy.setRoleGroup(roleGroup);
copy.setRole(role);
return copy;
}

Expand Down
Loading

0 comments on commit 5c84bf4

Please sign in to comment.