Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

Auto Updating User Roles With PeregrineBadges

qbauerqbauer
edited March 2013 in Vanilla 2.0 - 2.8

***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:

  1. Is this safe to enable this function in a live environment?
  2. Any tweaks to the code to make it safe?
  3. 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...)
  4. 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

Answers

  • Nice, hgtonight. I'm going to try this out...

  • Just what I'm looking for! :-)

    Thanks, and will be following your progress with this!

Sign In or Register to comment.