We'd like to dynamically change a users role via PHP. Specifically, we'd like to add any of several roles to a user when they change their password for the very first time, and again when the user visits a certain page (or takes some other action) for the very first time. Similarly, if they visit another page "n" times, they get another role added... and so forth. Thus roles "Has Changed Password" and "Has Read Legal" and "Has Played Game More than 100x" exist as roles, and these affect which blocks the user gets to play with.
I've gotten as far as ripping apart the $user object, where I can see the roles array... but that's as far as I get.
Do I simply add new roles into the array and issue some yet-to-be-discovered "save" command, or is there some kind of API I need to use or load? I'm new to Drupal, so please... speak... very... slowly... :)
Comments
_
I usually do this with the rules module-- maybe take a look at the code there to see how it's done.
you can do some of that with
you can do some of that with the triggered rules ( maybe just called rules ? ) module.
But in php , what you're going to have to is, (after creating all the roles)
Add an entry in the users_role table. You'll need to get familiar with the users, users_roles, and roles tables. Users_roles is where a role is associated with a user.
So, you'll need to use the INSERT command for Mysql
$q = INSERT INTO table (field) VALUES ('some value'_);
db_query($q);
or something like that. where your table and fields will be from users_roles. You'll need to get those values from the other tables.
So if you're new to php, you need to get to know db_query. There's help at api.drupal.org, to find out about functions, and basic web searches like 'mysql how to add a record' will help.
Mark
_
Just to clarify, the http://drupal.org/project/rules module is a contributed module that greatly enhances core triggers and actions.