修改_layout为不同的用户 [英] Modify _layout for different users

查看:122
本文介绍了修改_layout为不同的用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用我有菜单导航栏。在菜单中我有3个下拉列表。

In my App I have navbar with menu. In menu I have 3 dropdowns.


  • 公共

  • 对于普通用户

  • 的管理员

访问被限制,但菜单是所有可见。我想隐藏正常和公众(anynymous)用户不必要的元素。

Access is restricted but menus are visible for all. I want to hide unnecessary element for normal and public(anynymous) users.

作为identyfication我使用Windows的登录名

for identyfication I'm using windows login names

要获取用户的角色我问数据库和查询的回报,如果用户是普通用户或管理员。

To get a user role I'm asking database and query returns if user is normal user or admin.

我的解决办法:

public bool CheckIfAdmin(string login)
    {
        bool admin = false;
        EquipmentEntities db = new EquipmentEntities();
        Tuple<string, string> credentials = GetName(login);
        int RoleId = db.Users.Where(w => w.Name == credentials.Item1).Where(w => w.Surname == credentials.Item2).Select(s => s.RoleId).FirstOrDefault();
        if(RoleId==1)
        {
            admin = true;
        }
        return admin;
    }

和几乎相同的code的检查,如果用户

and nearly same code for for checking if User

在方法:

if(CheckIfAdmin(login)){
ViewBag.Role=1;
}
else if(CheckIfUser(login)){
    ViewBag.Role=2;
}

终于在布局:

 @if (ViewBag.Role==1)
        {
          <li class="dropdown">
            <a href="#" class="dropdown-toggle" data-toggle="dropdown">Admin<b class="caret"></b></a>
            <ul class="dropdown-menu">
                //MEnu
            </ul>
          </li>
         }

和几乎相同的code为第二个下拉我想隐藏。
这是工作,但我在这一刻我需要把检查每个方法的作用。其大量的冗余code的。任何人都可以建议我如何使它更好?

and nearly same code for second dropdown I want to hide. This is working but I at this moment I need to put checks for role in each method. Its large amount of redundant code. Can anyone suggest me how to make it better?

推荐答案

我看到你的角色是静态的,因为你检查如果(角色ID == 1),使用户为admin。我觉得你可以这样定义枚举角色。

I see your roles is static, because you check if(RoleId==1) so that user is admin. I think you can define roles like enum.

public enum UserRole
        {
            User = 1,
            Manager = 2,
            Admin = 3,
            //SuperAdmin...etc.
        }

创建基础控制器,增加的currentUser财产。而当Action的execute采取当前用户。

Create base controller, add CurrentUser property. And when action execute take current user.

 public class BaseController : Controller
   {
      protected override void OnActionExecuting(ActionExecutingContext filterContext)
      {
         CurrentUser = db.GetLoggedUserFromDatabase();  // to use in controller
         ViewBag.CurrentUser = CurrentUser;             // to use in views
      }

      public User CurrentUser { get; set; }   
   }

最后你的控制器实现 BaseController

 public class AnyController : BaseController 
{
       //in every action you have current user's details.
       //Already you know current users role. you can use it. for example:
       public ActionResult AnyAction()
       {
         if(CurrentUser != null)   //if user logged
         {
           if (CurrentUser.Role == (int)UserRole.Admin) 
           {
              //user is admin
           }   
         }      
       }
}

在意见你可以使用 ViewBag.CurrentUser 。投它第一次再检查作用于控制器。

In views you can use ViewBag.CurrentUser. Cast it first then check role as in controller.

这篇关于修改_layout为不同的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆