Skip to content

Commit

Permalink
make role auditable and retireable
Browse files Browse the repository at this point in the history
  • Loading branch information
mherman22 committed Jan 14, 2024
1 parent 6068865 commit 14890b6
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 22 deletions.
188 changes: 180 additions & 8 deletions api/src/main/java/org/openmrs/Role.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,21 @@

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.search.annotations.Field;
import org.openmrs.util.RoleConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

Expand All @@ -41,7 +40,7 @@

@Entity
@Table(name="role")
public class Role extends BaseOpenmrsObject {
public class Role extends BaseOpenmrsObject implements Auditable, Retireable {

public static final long serialVersionUID = 1234233L;

Expand All @@ -51,10 +50,6 @@ public class Role extends BaseOpenmrsObject {
@Column(name = "role")
private String role;

@Transient
@Field
private String name;

@Column(name = "description", length = 255)
private String description;

Expand Down Expand Up @@ -85,6 +80,33 @@ public class Role extends BaseOpenmrsObject {
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
private Set<Role> childRoles;

@ManyToOne
@JoinColumn(name = "creator", nullable = false)
private User creator;

@Column(name = "date_created", nullable = false, length = 19)
private Date dateCreated;

@ManyToOne
@JoinColumn(name = "changed_by")
private User changedBy;

@Column(name = "date_changed", length = 19)
private Date dateChanged;

@Column(name = "retired", nullable = false, length = 1)
private boolean retired;

@ManyToOne
@JoinColumn(name = "retired_by")
private User retiredBy;

@Column(name = "date_retired", length = 19)
private Date dateRetired;

@Column(name = "retire_reason", length = 255)
private String retireReason;

// Constructors

/** default constructor */
Expand Down Expand Up @@ -385,4 +407,154 @@ public Set<Role> recurseOverChildren(final Set<Role> total) {

return allRoles;
}

/**
* @since 2.7.0
* @return User - the user who last changed the Role
*/
@Override
public User getChangedBy() {
return changedBy;
}

/**
* @since 2.7.0
* @param changedBy - the user who last changed the Role
*/
@Override
public void setChangedBy(User changedBy) {
this.changedBy = changedBy;
}

/**
* @since 2.7.0
* @return Date - the date the Role was last changed
*/
@Override
public Date getDateChanged() {
return dateChanged;
}

/**
* @since 2.7.0
* @param dateChanged - the date the Role was last changed
*/
@Override
public void setDateChanged(Date dateChanged) {
this.dateChanged = dateChanged;
}

/**
* @since 2.7.0
* @return User - the user who created the Role
*/
@Override
public User getCreator() {
return creator;
}

/**
* @since 2.7.0
* @param creator - the user who created the Role
*/
@Override
public void setCreator(User creator) {
this.creator = creator;
}

/**
* @since 2.7.0
* @return Date - the date the Role was created
*/
@Override
public Date getDateCreated() {
return dateCreated;
}

/**
* @since 2.7.0
* @param dateCreated - the date the Role was created
*/
@Override
public void setDateCreated(Date dateCreated) {
this.dateCreated = dateCreated;
}

/**
* @since 2.7.0
* @return Boolean - whether of not this object is retired
* @deprecated as of 2.0, use {@link #getRetired()}
*/
@Override
public Boolean isRetired() {
return retired;
}

@Override
public Boolean getRetired() {
return retired;
}

/**
* @since 2.7.0
* @param retired - whether of not this Role is retired
*/
@Override
public void setRetired(Boolean retired) {
this.retired = retired;
}

/**
* @since 2.7.0
* @return User - the user who retired the Role
*/
@Override
public User getRetiredBy() {
return retiredBy;
}

/**
* @since 2.7.0
* @param retiredBy - the user who retired the Role
*/
@Override
public void setRetiredBy(User retiredBy) {
this.retiredBy = retiredBy;
}

/**
* @since 2.7.0
* @return Date - the date the Role was retired
*/
@Override
public Date getDateRetired() {
return dateRetired;
}

/**
* @since 2.7.0
* @param dateRetired - the date the Role was retired
*/
@Override
public void setDateRetired(Date dateRetired) {
this.dateRetired = dateRetired;
}

/**
* @since 2.7.0
* @return String - the reason the Role was retired
*/
@Override
public String getRetireReason() {
return retireReason;
}

/**
* @since 2.7.0
* @param retireReason - the reason the Role was retired
*/
@Override
public void setRetireReason(String retireReason) {
this.retireReason = retireReason;
}
}
2 changes: 1 addition & 1 deletion api/src/test/java/org/openmrs/api/UserServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1155,7 +1155,7 @@ public void saveRole_shouldAllowARoleToBeSavedWithCorrectPermissions() throws Il
Privilege myPrivilege = new Privilege("custom privilege");
role.addPrivilege(myPrivilege);

User currentUser = new User();
User currentUser = createTestUser();
currentUser.addRole(new Role(RoleConstants.SUPERUSER));

withCurrentUserAs(currentUser, () -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
<patient_identifier_type patient_identifier_type_id="1" name="Test Identifier Type" description="Test description" creator="1" date_created="2005-01-01 00:00:00.0" required="false" retired="false" uuid="9872ce12-59c1-47bd-b7ec-f5786e12830a"/>
<patient_identifier patient_identifier_id="1" patient_id="2" identifier="1234" identifier_type="1" preferred="1" location_id="1" creator="1" date_created="2005-01-01 00:00:00.0" voided="false" uuid="7eabecbb-9698-46e0-9abf-362a86d2dccd"/>
<privilege privilege="Some Privilege" description="This is a test privilege" uuid="d979d066-15e6-467c-9d4b-cb575ef97f0f"/>
<role role="Some Role" description="This is a test role" uuid="94d62f4b-dd14-4597-be10-402def31094c"/>
<role role="Some Role To Delete" description="This is a test role" uuid="24v32d4b-dd14-4597-be10-402def31094c"/>
<role role="Parent" description="This is a test parent role" uuid="8de52b9e-e07d-4978-ba4a-a3828c13e482"/>
<role role="Some Role" description="This is a test role" creator="1" date_created="2008-08-15 15:46:47.0" changed_by="1" date_changed="2008-08-15 15:47:07.0" retired="false" retire_reason="Test purposes" uuid="94d62f4b-dd14-4597-be10-402def31094c"/>
<role role="Some Role To Delete" description="This is a test role" creator="1" date_created="2008-08-15 15:46:47.0" changed_by="1" date_changed="2008-08-15 15:47:07.0" retired="false" retire_reason="Test purposes" uuid="24v32d4b-dd14-4597-be10-402def31094c"/>
<role role="Parent" description="This is a test parent role" creator="1" date_created="2008-08-15 15:46:47.0" changed_by="1" date_changed="2008-08-15 15:47:07.0" retired="false" retire_reason="Test purposes" uuid="8de52b9e-e07d-4978-ba4a-a3828c13e482"/>
<role_role parent_role="Parent" child_role="Some Role" />
<role_role parent_role="Parent" child_role="Some Role To Delete" />

Expand All @@ -31,4 +31,4 @@
<users user_id="6001" person_id="6001" system_id="6-7" username="userForChangePasswordChange" salt="3b293c5448d5e6a62a3db8734636bf185e95c9dc4d8e54d37e17073ec8046e8a566eb8046581f96a821bbe33f21d2fae28154343f0b59fedd2ed9e0d578ac6ae" password="8836e4c323e2f2df47ef5a1fcd599d8033e5daf66a414cc16faa20474df600acbecda9765fd7078b115e9f17662112eb6921e22826e8460e2a65b6e8bfd8abd4" secret_question="secret" secret_answer="cfd32f98c491f9f79803490923db520524e79fbfc885763421f8ad6440b04437405ecf5c73e7147ecd6f4d99080dd857a408218d0509305eb0fba22d7d2ad90d" creator="1" date_created="2008-08-15 15:57:09.0" changed_by="1" date_changed="2008-08-18 11:51:56.0" retired="false" retire_reason="" uuid="0bc18050-e132-11de-babe-001e378eb67f"/>


</dataset>
</dataset>
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
<patient_identifier_type patient_identifier_type_id="1" name="Test Identifier Type" description="Test description" creator="1" date_created="2005-01-01 00:00:00.0" required="false" retired="false" uuid="9872ce12-59c1-47bd-b7ec-f5786e12830a"/>
<patient_identifier patient_identifier_id="1" patient_id="2" identifier="1234" identifier_type="1" preferred="1" location_id="1" creator="1" date_created="2005-01-01 00:00:00.0" voided="false" uuid="7eabecbb-9698-46e0-9abf-362a86d2dccd"/>
<privilege privilege="Some Privilege" description="This is a test privilege" uuid="d979d066-15e6-467c-9d4b-cb575ef97f0f"/>
<role role="Some Role" description="This is a test role" uuid="94d62f4b-dd14-4597-be10-402def31094c"/>
<role role="Some Role To Delete" description="This is a test role" uuid="24v32d4b-dd14-4597-be10-402def31094c"/>
<role role="Parent" description="This is a test parent role" uuid="8de52b9e-e07d-4978-ba4a-a3828c13e482"/>
<role role="Some Role" description="This is a test role" creator="1" date_created="2008-08-15 15:46:47.0" changed_by="1" date_changed="2008-08-15 15:47:07.0" retired="false" retire_reason="Test purposes" uuid="94d62f4b-dd14-4597-be10-402def31094c"/>
<role role="Some Role To Delete" description="This is a test role" creator="1" date_created="2008-08-15 15:46:47.0" changed_by="1" date_changed="2008-08-15 15:47:07.0" retired="false" retire_reason="Test purposes" uuid="24v32d4b-dd14-4597-be10-402def31094c"/>
<role role="Parent" description="This is a test parent role" creator="1" date_created="2008-08-15 15:46:47.0" changed_by="1" date_changed="2008-08-15 15:47:07.0" retired="false" retire_reason="Test purposes" uuid="8de52b9e-e07d-4978-ba4a-a3828c13e482"/>
<role_role parent_role="Parent" child_role="Some Role" />
<role_role parent_role="Parent" child_role="Some Role To Delete" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
-->
<dataset>
<person person_id="1" gender="" birthdate="1975-06-30" birthdate_estimated="false" dead="false" date_created="2005-01-01 00:00:00.0" voided="false" uuid="6adb7c42-cfd2-4301-b53b-ff17c5654ff7"/>
<role role="Anonymous" description="Everyone and their brother should have this role" uuid="5b773b4d-90be-4d3f-81ca-cbb1441541ec"/>
<role role="Authenticated" description="Every person logged in automatically has this role." uuid="0dc8933e-7105-4c3b-b86c-15618d854660"/>
<role role="System Developer" description="Administrators of the OpenMRS .. have additional access to change fundamental structure of the database model." uuid="0e43640b-67d1-4458-b47f-b64fd8ce4b0d"/>
<users user_id="1" person_id="1" system_id="1-8" username="admin" password="4a1750c8607d0fa237de36c6305715c223415189" salt="c788c6ad82a157b712392ca695dfcf2eed193d7f" secret_question="" creator="1" date_created="2005-01-01 00:00:00.0" changed_by="1" date_changed="2007-09-20 21:54:12.0" retired="false" retire_reason="" uuid="1010d442-e134-11de-babe-001e378eb67e"/>
<users user_id="0" person_id="1" system_id="daemon" creator="1" date_created="2005-01-01 00:00:00.0" retired="false" retire_reason="" uuid="A4F30A1B-5EB9-11DF-A648-37A07F9C90FB"/>
<users user_id="1" person_id="1" system_id="1-8" username="admin" password="4a1750c8607d0fa237de36c6305715c223415189" salt="c788c6ad82a157b712392ca695dfcf2eed193d7f" secret_question="" creator="1" date_created="2005-01-01 00:00:00.0" changed_by="1" date_changed="2007-09-20 21:54:12.0" retired="false" retire_reason="" uuid="1010d442-e134-11de-babe-001e378eb67e"/>
<users user_id="0" person_id="1" system_id="daemon" creator="1" date_created="2005-01-01 00:00:00.0" retired="false" retire_reason="" uuid="A4F30A1B-5EB9-11DF-A648-37A07F9C90FB"/>
<role role="Anonymous" description="Everyone and their brother should have this role" creator="1" date_created="2008-08-15 15:46:47.0" changed_by="1" date_changed="2008-08-15 15:47:07.0" retired="false" retire_reason="Test purposes" uuid="5b773b4d-90be-4d3f-81ca-cbb1441541ec"/>
<role role="Authenticated" description="Every person logged in automatically has this role." creator="1" date_created="2008-08-15 15:46:47.0" changed_by="1" date_changed="2008-08-15 15:47:07.0" retired="false" retire_reason="Test purposes" uuid="0dc8933e-7105-4c3b-b86c-15618d854660"/>
<role role="System Developer" description="Administrators of the OpenMRS .. have additional access to change fundamental structure of the database model." creator="1" date_created="2008-08-15 15:46:47.0" changed_by="1" date_changed="2008-08-15 15:47:07.0" retired="false" retire_reason="Test purposes" uuid="0e43640b-67d1-4458-b47f-b64fd8ce4b0d"/>
<user_role user_id="1" role="System Developer"/>
</dataset>
Original file line number Diff line number Diff line change
Expand Up @@ -409,9 +409,9 @@
<relationship_type relationship_type_id="2" a_is_to_b="Parent" b_is_to_a="Child" preferred="0" weight="0" description="parent and child" creator="1" date_created="2008-08-15 15:55:18.0" retired="false" uuid="2a5f4ff4-a179-4b8a-aa4c-40f71956eabc"/>
<relationship relationship_id="1" person_a="502" relationship="1" person_b="2" creator="1" date_created="2008-08-18 11:50:15.0" voided="false" uuid="c18717dd-5d78-4a0e-84fc-ee62c5f0676a"/>
<relationship relationship_id="2" person_a="502" relationship="1" person_b="7" creator="1" date_created="2008-08-18 11:53:50.0" voided="false" uuid="8c0a3d9c-bf61-496b-95b0-72e6bc4a2b83"/>
<role role="Provider" description="General privileges held by all providers" uuid="3480cb6d-c291-46c8-8d3a-96dc33d199fb"/>
<users user_id="501" person_id="501" system_id="2-6" username="bruno" password="ff6d1655327d385e11a04e1632b4f33ceb8fafd2" salt="5d32c5fd9fde391c755f1f4dfd5e1d6e3debe6" secret_question="" creator="1" date_created="2008-08-15 15:46:47.0" changed_by="1" date_changed="2008-08-15 15:47:07.0" retired="true" retire_reason="Test purposes" uuid="c1d8f5c2-e131-11de-babe-001e378eb67e"/>
<users user_id="502" person_id="502" system_id="3-4" username="butch" password="eeeda5c0cc3837151b2d61cfeab54a91fb0c27d" salt="42af4c437a47cd778a54f6564d71b3cd6e8e5ca" secret_question="" creator="1" date_created="2008-08-15 15:57:09.0" changed_by="1" date_changed="2008-08-18 11:51:56.0" retired="false" retire_reason="" uuid="c98a1558-e131-11de-babe-001e378eb67e"/>
<role role="Provider" description="General privileges held by all providers" creator="1" date_created="2008-08-15 15:46:47.0" changed_by="1" date_changed="2008-08-15 15:47:07.0" retired="false" retire_reason="Test purposes" uuid="3480cb6d-c291-46c8-8d3a-96dc33d199fb"/>
<user_role user_id="501" role="Provider"/>
<user_role user_id="502" role="Provider"/>
<global_property property="locale.allowed.list" property_value="en_GB" uuid="4f55827e-26fe-102b-80cb-0017a47871b3"/>
Expand Down

0 comments on commit 14890b6

Please sign in to comment.