Need user levels to intersect permissions with anonymous

This public forum is for user-to-user discussions of PHPMaker. Note that this is not support forum.
Post Reply
ppinto
User
Posts: 138

Need user levels to intersect permissions with anonymous

Post by ppinto »

Hi all,

I have a project with dynamic user levels and lots of tables and lots of diferent user levels. It's a pain to set all the permissions everytime I need to create another user level, especially because a lot of tables are seen by the anonymous user level and other user levels have at least the same permissions.
At login, I'd like to intersect the permissions of the user's level (1, for instance) with the ones already set for level -2 (anonymous) without having them explicitly set for that level (1). After all, if someone can do something on a given table when not logged in, so should the people that are logged in.
Another advantage: if I need to alter the permissions for anonymous level, I have to do the same to all the other levels, and that really IS a pain.
The only drawback that I see is losing the ability to block access to a given area for people who are logged in, but that doesn't apply to my case (and I think this is true for most people).
Any ideas?

Pedro


sangnandar
User
Posts: 980

Post by sangnandar »

First of all, you can stack userlevel.
For example using
UserLevel_Loaded(){
if (CurrentUserLevel() == 1) {
$this->AddUserLevelID(-2);
}
}

Note that, the way PHPM works, UserLevel is not scalar, it's array.
CurrentUserLevel(); // return the scalar value defined in user table (user.userlevelid). This is initial value.
CurrentUserLevels(); // return array, the stacked value, initial value + values added latter during runtime.

So, in above example, userlevel 1 will now have [1,-2] in his array. Meaning: he has 1 and -2 userlevelpermissions.
This is a great flexibility. You can freely add/remove userlevel based on condition during runtime.


sangnandar
User
Posts: 980

Post by sangnandar »

If you just need to copy userlevelpermissions from -2 to 1, have no plan to stack 1 and -2 in the future, both goes different way.
Instead of clicking permission-boxes in apps you can directly issued sql on db-side:
// insert userlevel
insert into userlevels (userlevelid,userlevelname) values (1, 'Level-1')
// copy permissions
insert into userlevelpermissions (userlevelid,tablename,permission) select 1,tablename,permission from userlevelpermissions where userlevelid = -2


ppinto
User
Posts: 138

Post by ppinto »

Thanks for the info, sangnandar.

This seems perfect, but it doesn't work as yet.
I made all userlevels equal or above 1 to stack -2. But the menus didn't change as expected, showing the extra tables.
Maybe something else needs to be done.

Pedro

sangnandar wrote:
First of all, you can stack userlevel.
For example using
UserLevel_Loaded(){
if (CurrentUserLevel() == 1) {
$this->AddUserLevelID(-2);
}
}

Note that, the way PHPM works, UserLevel is not scalar, it's array.
CurrentUserLevel(); // return the scalar value defined in user table
(user.userlevelid). This is initial value.
CurrentUserLevels(); // return array, the stacked value, initial value + values added
latter during runtime.

So, in above example, userlevel 1 will now have [1,-2] in his array. Meaning: he has
1 and -2 userlevelpermissions.
This is a great flexibility. You can freely add/remove userlevel based on condition
during runtime.


sangnandar
User
Posts: 980

Post by sangnandar »

Output userlevels to see if you have the array right.
Note that above sample code is v2018, check if they have changed for v2019.

You can find
function AddUserLevelID(){
}
in phpfn*.php


ppinto
User
Posts: 138

Post by ppinto »

I have function UserLevel_Loaded like this:

	if ($this->CurrentUserLevelID() >= 1) {
		$this->AddUserLevelID(-2);
	}

Then, I spotted this condition in function AddUserLevelID:

	if ($UserLevelID < -1) return;

Changed it to:

	if ($UserLevelID < -2) return;

Now as expected, after logging in, calling CurrentUserLevels() returns [1, -2]. Still, can't see or access the tables alowed by -2.
Little extra help? :)


ppinto
User
Posts: 138

Post by ppinto »

Also tried adding level 0 instead of -2, to avoid changes to function AddUserLevelID. Same result.


sangnandar
User
Posts: 980

Post by sangnandar »

Well... obviously because 0, -1, and -2 are special userlevels.
Sorry can't help with that, I've never stacked this userlevels before. Need to go through generated scripts to see why does it happen.

Worst approach,

  1. Create dummy userlevels act as 0 and -2 in userlevelpermsissons table. Let's say: 0 => 100, -2 => 98.
  2. Create stored-procedure against table:userlevelpermissions that copy-paste 0 and -2 into 100 and 98.
  3. Call this SP, manually, each time you update 0 and -2 permissions.

ppinto
User
Posts: 138

Post by ppinto »

Well, not so obviously, because function AddUserLevelID allows user levels >= -1, so at least administrator (-1) and default (0) could be stacked.
Nevertheless, I took your advice and created a new user level as a copy of -2, set the desired permissions and changed the code inside UserLevel_Loaded() accordingly:

	if ($this->CurrentUserLevelID() >= 1) {
		$this->AddUserLevelID(6); //6 is the new user level, created as a copy of 'Anonymous'
	}

Same result. Function AddUserLevelID doesn't add the level as intended...


sangnandar
User
Posts: 980

Post by sangnandar »

Oopss, sorry.
I checked my code and it should be TablePermission_Loading() server event, not UserLevel_Loaded().
IIRC, this is because the array is not yet existed during UserLevel_Loaded().


ppinto
User
Posts: 138

Post by ppinto »

It worked!
Thanks, sangnandar.

Cheers


Post Reply