How to get the role of a user? - drupal

I have 2 roles in my drupal site. One is admin. The uid is 1. If I visit the profile of another user, say a user with uid 2, is it possible to get that value. I did not want to retrieve it from the url(user/2). Is there any other way to get a user information like uid, role etc while visiting profile of another user. I think $user can be used to get the details of current user only.

To get the details from user id when you are in the profile page of that user use the $account variable. More details in the user-profile.tpl.php template inside ROOT/modules/user.
Example, get user id: $account->uid;

Related

How can I get a user role history?

Is there any way to get a user role history? Let's say I added a new user with the role Subscriber. Now I manually updated the role to "Editor", and then "Administrator".
Is there any way to get the history of the user with date/time?
For e.g:
User 1 was a "Subscriber" on 25/01/2023
User 1 updated to "Editor" on 27/01/2023.
User 1 updated to "Administrator" on 29/01/2023.
I know we can fetch the current display role
There's nothing in core WordPress that records this history. There are some popular history plugins you might try. https://wordpress.org/plugins/search/history/
The current role settings for each user are stored in wp_usermeta in an entry with meta_key = 'wp_capabilities'. For example, an editor will have this meta value:
array( 'editor' => true )
Recording changes to those metadata values will give you the history if you decide to do this with your own plugin code. You can hook the 'update_user_meta', 'add_user_meta', and 'delete_user_meta' actions to intercept changes.

How to get Roles from using SimpleMembership?

I am developing a MVC4 application with SimpleMembership. I have a table - "userInfo" in which I am storing user's information such as Name, Email, Address, Phone, Role etc. When I register a user, data is stored in this table and webpages_Membership. No data is stored in other Membership tables (OAuthMembership, Roles, UserInRoles).
When I login a user, it is validated using :
if (ModelState.IsValid && WebSecurity.Login(Model.Name, Model.Password, false))
it returns "True" but after this, I need to get the role of the registered user.
In SimpleMembership, does "Roles and UserInRoles" table provide registered user role or can I query the "userInfor" table and get roles from this table.
Please advice
Thanks in Advance
to get all available roles, assuming you have enabled Roles and added at least one..
var roles = (SimpleRoleProvider)Roles.Provider;
var allRoles = roles.GetAllRoles();
to get specific user's roles.
var userRoles = roles.GetRolesForUser("specificusername");
ref MSDN
Simple Membership does not come with any out of the box management pages for Roles. You are on your own to create them, or manage them directly through code/sql/ef etc..
Code examples...
Check for and creation of Admin role:
if (!Roles.RoleExists("Admin"))
Roles.CreateRole("Admin");
Adding user to role on creation:
if (!Roles.GetRolesForUser("specificusername").Contains("Admin"))
Roles.AddUsersToRoles(new[] {"specificusername"}, new[] {"Admin"});
ref adding-security-and-membership
You can user Roles.GetRolesForUser Method after your user logged in
Gets a list of the roles that the currently logged-on user is in.
Or if you want to check whether current user is in specified role you can use Roles.IsUserInRole Method

Revoke User Role from inherited Group

I'm using FOSUserBundle and set up a User / Group environment.
I can give Roles to Groups and Users.
Basically I give the Roles to the Group.
Now, I want to revoke some Users in a Group a Role that is inherited from the Group.
Every time I unselect it in the UserAdminView ( SonataUserBundle ) it is rechecked again ( for sure, because it gets it from the Group)
Any Idea how to achieve that?
I thought about a extra field in the Usertable where all Roles that should be Revoked are listed.
Is there an easier opportunity?
What you're asking for is beyond the scope of FOSUserBundle, as far as I know. No matter what, when a user logs in, it's going to load all of the roles assigned to a specific user, and all of the roles in the groups a user is assigned to.
You may try to create a custom field under the User entity called 'denied_roles' or something of that nature. Then create a custom login listener (listening to 'security.interactive_login') that removes the 'denied_roles'.

user_load by a user profile field?

I'm trying to login users by detecting it's facebook user id.
"profile_fbuid" is a (hidden) profile field that I created to login users with the corresponding facebook user id.
When a user tries to login with Facebook I detect his/her facebook user id, but when I try to match with corresponding Drupal user this line doesn't work:
$user_exists = user_load(array('profile_fbuid'=>$fbuserid));
I get this error: user warning: Unknown column 'profile_fbuid'
I know what the error means, but I don't know how can I search a user using a user profile field.
Thanks for your help!
You created a custom field that is attached to the user profile called profile_fbuid. What you want to do in the database is look for this table:
field_data_field_profile_fbuid.
The relevant columns are entity_id, which will be the (Drupal) user id, and field_profile_fbuid_value, which will contain the value (the Facebook UID).
There is no field in the users table called profile_fbuid, so I assume you generated this field by using the user profile module.
I think it's better to create a query which gets the uid of this column and then passes it to the user_load() function.
$query = db_query("select `uid` from {table_name} where `profile_fbuid` = %d ",$fbuserid );
while($result = db_fetch_object($query)){
$uid = $result->uid ;
$user_exists = user_load(array('uid'=>$uid));
}

Give a user rights to a specific node in Drupal

I'd like the user "student" to be assigned to a content type "Projectgroup". I can do this by adding a user reference to the Projectgroup content type form.
Example:
Projectgroup = Beta testers
Students (user referenced) = Kim, Joel, John.
When Kim logs in, she should only be able to post as "Beta testers".
How can i make sure when the user logs in he/she can only post with the correct projectgroup rights?
You want to assign roles to these students and then control what permissions are available to that role.
http://drupal.org/handbook/modules/user

Categories

Resources