Skip to content

Commit

Permalink
#768 Enhanced user-access management.
Browse files Browse the repository at this point in the history
  • Loading branch information
lawal-olaotan committed Sep 10, 2023
1 parent 8fa5c3f commit b06739c
Showing 1 changed file with 23 additions and 31 deletions.
54 changes: 23 additions & 31 deletions client-base/src/main/webapp/html/javascript/user-access.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,15 @@ class UserAccessManager{
* Should make things slightly more efficient.
*
*/

#current = null;
#defaultGeneLimit = null;
#defaultKnetViewLimit = null;
#isGeneLimitEnforced = null;
#defaultGeneLimit = 20;
#defaultKnetViewLimit = 10;
#isGeneLimitEnforced = true;

constructor(){
this.#current = 'guest'
this.#defaultGeneLimit = 20;
this.#defaultKnetViewLimit = 10;
this.#isGeneLimitEnforced = true;
UserRole.setUserRole('guest');
}


// Calls knetspace API endpoint and returns user current plan (free or pro) as a string.
setUserPlan()
{
Expand All @@ -35,16 +31,16 @@ class UserAccessManager{
const data = await response.json();
const userPlan = data.plan?.name.toLowerCase()
if(userPlan?.length){
this.#current = userPlan
this.#setGeneSearchLimit();
UserRole.setUserRole(userPlan);
this.setGeneSearchLimit();
}

})
}

// Sets geneslist search limit based on current user plan.
// 20 for guest, 100 for free and unlimited for pro users.
#setGeneSearchLimit()
setGeneSearchLimit()
{
/*
* TODO: this is logically wrong. Once you've introduced roles as
Expand All @@ -69,20 +65,11 @@ class UserAccessManager{
*
*/

if( this.#current === 'free' )
{
this.#defaultGeneLimit = 100;

// sets current genelist limit
$('.genesCount').html(`0/${this.#defaultGeneLimit}`)

}else if(this.#current === 'pro'){
if(this.requires('free')) this.#defaultGeneLimit = 100;

if(this.requires('pro')){
this.#isGeneLimitEnforced = false;
this.#defaultKnetViewLimit = 20
// TODO: don't mix business logic and UI
// Have a separated function/component, like refreshUIrestrictions() and
// call it outside of this class, after updates like setUserPlan()
$('.genesCount').hide();
}
}

Expand Down Expand Up @@ -121,7 +108,7 @@ class UserAccessManager{
See above about #current, and see below about can()
*/
return UserRole.can( this.#current, queryRole);
return UserRole.can(queryRole);
}


Expand All @@ -132,6 +119,7 @@ class UserAccessManager{
*/
class UserRole {
#level = null
userLevel = null;

static GUEST = new UserRole ( 1000 )
static REGISTERED = new UserRole ( 500 )
Expand All @@ -151,12 +139,11 @@ class UserRole {
* Compare by level, returns -1 | 0 | 1, ie, negative means this role is more
* powerful than the other.
*/
static compare(role,queryRole){
static compare(queryRole){

if ( !role) return -1
let userLevel = UserRole.get(role)
if ( !this.userLevel) return -1
let queryLevel = UserRole.get ( queryRole )
return queryLevel - userLevel
return queryLevel - this.userLevel

}

Expand All @@ -181,9 +168,9 @@ class UserRole {
* True if the role parameter has the same or higher power of this role.
* Param can be a UserRole or a string.
*/
static can ( role,queryRole )
static can (queryRole )
{
return UserRole.compare(role,queryRole) >= 0;
return UserRole.compare(queryRole) >= 0;
}

/**
Expand All @@ -206,6 +193,11 @@ class UserRole {
}
return result;
}

static setUserRole(roleStr){
const userLevel = UserRole.get(roleStr)
this.userLevel = userLevel
}
}

const userAccessMgr = new UserAccessManager()
Expand Down

0 comments on commit b06739c

Please sign in to comment.