Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get current user's role

Tags:

asp.net-mvc

Is there any way to get the explicit role that a user belongs to in my controller? This assumes using ASP.NET Membership and Role Providers. "IsInRole" doesn't work - I need to actually get the name of the role they are in.

like image 518
Ciel Avatar asked Jan 14 '10 23:01

Ciel


People also ask

How can I get current user role?

First off, we check that the user is actually logged in. If they're not logged in, they won't have a role assigned. If the user is logged in, we use wp_get_current_user to return the WP_User object. This provides us with a stack of information about the data and we can access their user role(s) via $user->roles .

How do I get current user in WordPress?

Getting all the information of current user in wordpress using the predefined function wp_get_current_user(); <? php $current_user = wp_get_current_user(); echo "Username :". $current_user->user_login; echo "Username :".

How do I show user roles in WordPress?

To check if a user has a specific role, you have to get a list of their roles and see if the role is listed there. $user_meta=get_userdata($user_id); $user_roles=$user_meta->roles; if (in_array("subscriber", $user_roles)){} Results in a role check for subscriber.

How do you get user roles in Outsystems?

If you want to use it in your visible property of any screen widget then you can create an action using "Fetch Data from Other Sources" which is available on screen when you right click. In this action your service side role actions will be available which you can call and assign their result to your output variable.


1 Answers

A user can be in more than one role so you can't get the one role that the user is in, but you can easily get the list of roles a user is in.

You can use the Roles type to get the list of roles that the currently logged in user is in:

public ActionResult ShowUserRoles() {     string[] roleNames = Roles.GetRolesForUser();     return View(roleNames); } 

Or if you want to get the roles for an arbitrary user you can pass in the username when you call Roles.GetRolesForUser().

like image 121
Eilon Avatar answered Oct 04 '22 17:10

Eilon