Auto Updating User Roles With PeregrineBadges
***Just a quick warning on the following code, test this ONLY in a development/test environment (I'm testing this on my localhost), especially if you don't know what you're doing. It would be possible to overwrite the admin role and lock yourself out of your own forum. But I figured out a simple way to automatically update a role with the following logic (it ends up overwriting the previous roles; if someone knows how NOT to overwrite the preexisting User Role, and instead just add the new role to the preexisting array of User Roles, please let me know; it should be fairly rudimentary, just pulling data out of an array, but my php is really rusty).
Here's the basic code (taken from the usermodel module):
$UserModel = new UserModel(); $UserRoles = $UserModel->GetRoles($UserID)->Result(); //I added this as I am trying to remember how to pull data out of an array, and then add a new variable to the end of the array, which would be better than overwriting the User Role // I added the if statement to check if the user ID is 1, which should be the admin, and not trigger it if it is the admin if ($UserID != 1) { $UserModel->SaveRoles($UserID,'33',FALSE); //this line overwrites the user role/ALL OF THE USER ROLES to 33, which in my install is the id number for a role I created called Veteran which has the same parameters as the Member role }
Here's how I'm thinking about using it (with the Peregrine Badges plugin)...in the class.peregrinebadgesmodule.php, I could trigger this code when Total Points reaches a certain limit, say 100. The logic may be something like: If Total Points equals 100, then make Total Points equal to 105, trigger the User script through the SaveRoles function, and optionally enable a new User Badge (like the 'Veteran Badge').
This should work, but I'd like some feedback, if anyone has time of course. I'd like to know:
- Is this safe to enable this function in a live environment?
- Any tweaks to the code to make it safe?
- Is there a better way to save the roll ADDITIVELY to an array of roles, instead of replacing the existing roles? (I kind of already answered my own question, but if anyone has a clean implementation of adding a new User Role to the array of roles...)
- Is there a better way to implement this code in order to make sure there are no weird loops; maybe something dangerous that I'm missing in the Total Points check. And is using SaveRoles to send this data to the database okay/safe with this method, or does this open the forum up to exploits...that kind of stuff would be useful...?
Have fun with the code!
Addendum: to reiterate since this idea was in another thread, this would be used to mimic 'ranks' or automatic role promotion for specific behaviors, ie a user getting promoted from a 'Noob' role to a full-fledged 'Member' role after successfully completing a number of comments, and receiving thanks, thereby opening up new member privileges (uploading avatars, uploading files, and so on)...also, this could be used to attach a new role (working like a rank with certain privileges) AND a badge at a certain Total Points threshold.
Best Answer
-
hgtonight MVP
Since this is going to be a common function in your plugin, I would write an AddRole() method. Example:
public function AddRole($UserID, $RoleID) { if(is_numeric($UserID) && is_numeric($RoleID) ) { $UserModel = new UserModel(); $UserRoleIDs = $UserModel->GetRoles($UserID)->Result(); if(!in_array($NewRoleID, $UserRoleIDs)) { array_push($UserRoleIDs, $NewRoleID); SaveRoles($UserID, $UserRoleIDs); return TRUE; } return FALSE; }
I haven't tested this, so treat it as pseudo code.
Check out the Documentation! We are always looking for new content and pull requests.
Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.
6
Answers
Since this is going to be a common function in your plugin, I would write an AddRole() method. Example:
I haven't tested this, so treat it as pseudo code.
Search first
Check out the Documentation! We are always looking for new content and pull requests.
Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.
Nice, hgtonight. I'm going to try this out...
Just what I'm looking for! :-)
Thanks, and will be following your progress with this!