Skip to content

Commit

Permalink
Show institution admin role in user search
Browse files Browse the repository at this point in the history
  • Loading branch information
oharsta committed Oct 7, 2023
1 parent 94a7da9 commit 4eb6662
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 7 deletions.
10 changes: 9 additions & 1 deletion client/src/__tests__/utils/UserRole.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
allowedAuthoritiesForInvitation,
allowedToDeleteInvitation,
allowedToEditRole,
highestAuthority,
allowedToRenewUserRole,
AUTHORITIES,
isUserAllowed
Expand Down Expand Up @@ -91,4 +92,11 @@ test("Allowed to edit", () => {
userRoles: [{authority: AUTHORITIES.INVITER, role: role}]
}
expect(allowedToEditRole(user, role)).toBeFalsy();
})
});

test("Highest authority", () => {
const user = {
institutionAdmin: true,
}
expect(highestAuthority(user, false)).toEqual(AUTHORITIES.INSTITUTION_ADMIN);
});
2 changes: 1 addition & 1 deletion client/src/components/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export const User = ({user, other, config}) => {
{attributes.map((attr, index) => attribute(index, attr[0], attr[1]))}

<h3 className={"title span-row "}>{I18n.t("users.roles")}</h3>
{highestAuthority(user) === AUTHORITIES.GUEST &&
{highestAuthority(user, false) === AUTHORITIES.GUEST &&
<p className={"span-row"}
dangerouslySetInnerHTML={{__html: DOMPurify.sanitize(I18n.t("users.guestRoleOnly", {welcomeUrl: config.welcomeUrl}))}}/>}
{(!hasRoles && user.superUser) &&
Expand Down
2 changes: 1 addition & 1 deletion client/src/tabs/Users.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export const Users = () => {
searchUsers(query)
.then(results => {
setInitial(false);
results.forEach(user => user.highestAuthority = highestAuthority(user));
results.forEach(user => user.highestAuthority = highestAuthority(user, false));
setUsers(results);
setMoreToShow(results.length === 15 && query !== "owl");
setNoResults(results.length === 0);
Expand Down
4 changes: 2 additions & 2 deletions client/src/utils/UserRole.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ const AUTHORITIES_HIERARCHY = {
[AUTHORITIES.GUEST]: 5
}

export const highestAuthority = user => {
export const highestAuthority = (user, forceApplications = true) => {
if (user.superUser) {
return AUTHORITIES.SUPER_USER;
}
if (user.institutionAdmin && !isEmpty(user.applications)) {
if (user.institutionAdmin && (!isEmpty(user.applications) || !forceApplications)) {
return AUTHORITIES.INSTITUTION_ADMIN;
}
return (user.userRoles || []).reduce((acc, u) => {
Expand Down
6 changes: 6 additions & 0 deletions server/src/main/java/access/model/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ public User(boolean superUser, Map<String, Object> attributes) {
this.createdAt = Instant.now();
this.lastActivity = this.createdAt;

this.nameInvariant(attributes);
}

private void nameInvariant(Map<String, Object> attributes) {
String name = (String) attributes.get("name");
String preferredUsername = (String) attributes.get("preferred_username");
if (StringUtils.hasText(name)) {
Expand Down Expand Up @@ -181,6 +185,8 @@ public void updateAttributes(Map<String, Object> attributes) {
this.familyName = (String) attributes.get("family_name");
this.email = (String) attributes.get("email");
this.lastActivity = Instant.now();

this.nameInvariant(attributes);
this.updateRemoteAttributes(attributes);
}

Expand Down
8 changes: 6 additions & 2 deletions server/src/test/java/access/model/UserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ class UserTest {

@Test
void fromAttributes() {
User user = new User(false, Map.of(
Map<String, Object> attributes = Map.of(
"email", "[email protected]"
));
);
User user = new User(false, attributes);
assertEquals("John Doe", user.getName());

user.updateAttributes(attributes);
assertEquals("John Doe", user.getName());

user = new User(false, Map.of(
Expand Down

0 comments on commit 4eb6662

Please sign in to comment.