Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using OOTB in react project #119

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ui/.env.development
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
NEXT_PUBLIC_OOTB_MODE=false
# Profiles: user, owner, admin
NEXT_PUBLIC_OOTB_PROFILE=admin
283 changes: 283 additions & 0 deletions ui/ootb.active.user.profiles.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,283 @@
[
{
"uid": "member0123",
"uhUuid": "11111111",
"authorities": ["ROLE_UH"],
"attributes": {
"cn": "MEMBER",
"mail": "[email protected]",
"givenName": "user"
},
"groupings": [
{
"name": "admin-include:exclude",
"displayName": "admin-include:exclude",
"extension": "exclude",
"displayExtension": "exclude",
"description": "Admin owned include group",
"members": [
{
"name": "MemberUser",
"uid": "member0123",
"uhUuid": "11111111"
}
]
},
{
"name": "member-group:owners",
"displayName": "member-group:owners",
"extension": "owners",
"displayExtension": "owners",
"description": "Member owned group",
"members": []
},
{
"name": "member-group:include",
"displayName": "member-group:include",
"extension": "include",
"displayExtension": "include",
"description": "Member owned group",
"members": [
{
"name": "complex-member7",
"uhUuid": "19283746",
"uid": "cmember7"
},
{
"name": "complex-member10",
"uhUuid": "01234567",
"uid": "cmember10"
},
{
"name": "AdminUser",
"uid": "admin0123",
"uhUuid": "33333333"
}
]
}
]
},
{
"uid": "owner0123",
"uhUuid": "22222222",
"authorities": ["ROLE_UH", "ROLE_OWNER"],
"attributes": {
"cn": "OWNER",
"mail": "[email protected]",
"givenName": "owner"
},
"groupings": [
{
"name": "shared-group-in-each-profile:owners",
"displayName": "shared-group-in-each-profile:owners",
"extension": "owners",
"displayExtension": "owners",
"description": "This is a shared group in each profile",
"members": [
{
"name": "shared-owner-2",
"uhUuid": "29325231",
"uid": "sowner2"
}
]
},
{
"name": "admin-include:include",
"displayName": "admin-include:include",
"extension": "include",
"displayExtension": "include",
"description": "Admin owned include group",
"members": [
{
"name": "OwnerUser",
"uid": "owner0123",
"uhUuid": "22222222"
},
{
"name": "AdminUser",
"uid": "admin0123",
"uhUuid": "33333333"
}
]
},
{
"name": "owner-group:exclude",
"displayName": "owner-group:exclude",
"extension": "exclude",
"displayExtension": "exclude",
"description": "Owner owned group",
"members": [
{
"name": "complex-member3",
"uhUuid": "56473829",
"uid": "cmember3"
},
{
"name": "complex-member4",
"uhUuid": "45261378",
"uid": "cmember4"
},
{
"name": "OwnerUser",
"uid": "owner0123",
"uhUuid": "22222222"
}
]
}
]
},
{
"uid": "admin0123",
"uhUuid": "33333333",
"authorities": ["ROLE_ADMIN", "ROLE_UH", "ROLE_OWNER"],
"attributes": {
"cn": "ADMIN",
"mail": "[email protected]",
"givenName": "admin"
},
"groupings": [
{
"name": "shared-group-in-each-profile:basis",
"displayName": "shared-group-in-each-profile:basis",
"extension": "basis",
"displayExtension": "basis",
"description": "This is a shared group in each profile",
"members": [
{
"name": "shared-owner-3",
"uhUuid": "29382734",
"uid": "sowner3"
},
{
"name": "MemberUser",
"uid": "member0123",
"uhUuid": "11111111"
}
]
},
{
"name": "shared-group-in-groupings:owners",
"displayName": "shared-group-in-groupings:owners",
"extension": "owners",
"displayExtension": "owners",
"description": "This is a shared group in admin user groupings",
"members": [
{
"name": "AdminUser",
"uid": "admin0123",
"uhUuid": "33333333"
},
{
"name": "OwnerUser",
"uid": "owner0123",
"uhUuid": "22222222"
}
]
},
{
"name": "shared-group-in-each-profile:owners",
"displayName": "shared-group-in-each-profile:owners",
"extension": "owners",
"displayExtension": "owners",
"description": "This is a shared group in each profile",
"members": [
{
"name": "shared-owner-3",
"uhUuid": "29382734",
"uid": "sowner3"
}
]
},
{
"name": "admin-include:owners",
"displayName": "admin-include:owners",
"extension": "owners",
"displayExtension": "owners",
"description": "Admin owned include group",
"members": []
},
{
"name": "admin-include:include",
"displayName": "admin-include:include",
"extension": "include",
"displayExtension": "include",
"description": "Admin owned include group",
"members": [
{
"name": "AdminUser",
"uid": "admin0123",
"uhUuid": "33333333"
}
]
},
{
"name": "admin-group:owners",
"displayName": "admin-group:owners",
"extension": "owners",
"displayExtension": "owners",
"description": "Admin owned group",
"members": []
},
{
"name": "owner-complex:owners",
"displayName": "Owner-Complex: Owners",
"extension": "owners",
"displayExtension": "Owners",
"description": "Owner's owned complex group",
"members": [
{
"name": "complex-member1",
"uhUuid": "32532314",
"uid": "cmember1"
},
{
"name": "complex-member2",
"uhUuid": "87453218",
"uid": "cmember2"
},
{
"name": "complex-member3",
"uhUuid": "56473829",
"uid": "cmember3"
},
{
"name": "complex-member4",
"uhUuid": "45261378",
"uid": "cmember4"
},
{
"name": "complex-member5",
"uhUuid": "98765432",
"uid": "cmember5"
},
{
"name": "complex-member6",
"uhUuid": "12345678",
"uid": "cmember6"
},
{
"name": "complex-member7",
"uhUuid": "19283746",
"uid": "cmember7"
},
{
"name": "complex-member8",
"uhUuid": "72635489",
"uid": "cmember8"
},
{
"name": "complex-member9",
"uhUuid": "65432109",
"uid": "cmember9"
},
{
"name": "complex-member10",
"uhUuid": "01234567",
"uid": "cmember10"
}
]
}
]
}
]

1 change: 0 additions & 1 deletion ui/src/components/layout/navbar/dept-account-icon.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use client';

import { useState } from 'react';

import DynamicModal from '@/components/modal/dynamic-modal';
import { faUser, faSchool } from '@fortawesome/free-solid-svg-icons';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
Expand Down
36 changes: 20 additions & 16 deletions ui/src/components/layout/navbar/login-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,29 @@

import { Button } from '@/components/ui/button';
import Role from '@/lib/access/role';
import User from '@/lib/access/user';
import { login, logout } from 'next-cas-client';
import User from '@/lib/access/user';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faSignInAlt, faSignOutAlt } from '@fortawesome/free-solid-svg-icons';

const LoginButton = ({ currentUser }: { currentUser: User }) => (
<>
{!currentUser.roles.includes(Role.UH) ? (
<Button variant="default" onClick={() => login()}>
Login <FontAwesomeIcon className="ml-1" icon={faSignInAlt} />
</Button>
) : (
<Button variant="outline" onClick={() => logout()}>
Logout&nbsp;
<span className="sm:inline hidden">({currentUser.uid})</span>{' '}
<FontAwesomeIcon className="ml-1" icon={faSignOutAlt} />
</Button>
)}
</>
);
const LoginButton = ({ currentUser }: { currentUser: User }) => {
const isOotbMode = process.env.NEXT_PUBLIC_OOTB_MODE === 'true';

return (
<>
{!currentUser?.roles.includes(Role.UH) && !isOotbMode ? (
<Button size="lg" variant="default" onClick={() => login()}>
Login <FontAwesomeIcon className="ml-1" icon={faSignInAlt} />
</Button>
) : (
<Button size="lg" variant="default" onClick={() => logout()} disabled={isOotbMode}>
Logout&nbsp;
<span className="sm:inline hidden">({currentUser.uid})</span>{' '}
<FontAwesomeIcon className="ml-1" icon={faSignOutAlt} />
</Button>
)}
</>
);
};

export default LoginButton;
40 changes: 38 additions & 2 deletions ui/src/lib/access/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import Role from './role';
import { CasUser } from 'next-cas-client';
import { setRoles } from './authorization';
import { getCurrentUser } from 'next-cas-client/app';

import { matchProfile, updateActiveDefaultUser } from '@/lib/actions-ootb';
import { OotbActiveProfile } from '../types';
type User = {
roles: Role[];
} & MemberResult;
Expand All @@ -28,9 +29,44 @@ export const loadUser = async (casUser: CasUser): Promise<User> => {
} as User;

await setRoles(user);
console.log('CAS user after setRoles:', user);
return user;
};

export const loadOotbUser = async (profile: OotbActiveProfile): Promise<User> => {
const user = {
name: profile.attributes.cn,
firstName: profile.attributes.givenName,
lastName: profile.attributes.sn,
uid: profile.uid,
uhUuid: profile.uhUuid,
roles: [Role.ANONYMOUS, ...convertAuthoritiesToRoles(profile.authorities)]
} as User;

return user;
};

export const getUser = async (): Promise<User> => (await getCurrentUser<User>()) ?? AnonymousUser;
const convertAuthoritiesToRoles = (authorities: string[]): Role[] => {
return authorities.map(authority => authority.replace(/^ROLE_/, '').toUpperCase())
.filter(roleName => Object.values(Role).includes(roleName as Role))
.map(roleName => roleName as Role);
};


export const getUser = async (): Promise<User> => {
if (process.env.NEXT_PUBLIC_OOTB_MODE === 'true') {
const givenName = process.env.NEXT_PUBLIC_OOTB_PROFILE;
try {
await updateActiveDefaultUser(givenName);
const profile = await matchProfile(givenName);
return profile ? await loadOotbUser(profile) : AnonymousUser;
} catch (error) {
console.error('Error fetching OOTB user:', error);
return AnonymousUser;
}
}
const user = (await getCurrentUser<User>()) ?? AnonymousUser;
return user;
};

export default User;
Loading
Loading