简体   繁体   English

通过功能/方法启用/禁用按钮

[英]Enabling/disabling buttons via function/method

  1. I have 8 buttons, each performs a different task, ie edit, delete, create etc, and a context​Menu for each of the task我有 8 个按钮,每个按钮执行不同的任务,即编辑、删除、创建等,以及每个任务的上下文菜单

  2. I've a table called Moderations in DB, which consists of bools ie groupTitle, canEdit, canDelete, canCreate..... groupTitle is string not bool我在 DB 中有一个名为Moderations的表,它由 bool 组成,即 groupTitle、canEdit、canDelete、canCreate..... groupTitle 是字符串而不是 bool

  3. I have a bool function canDoIt(task, userid) to check whether the logged in user (which will have specific groupTitle), can perform or can't (function return true or false for provided task, in short)我有一个 bool 函数canDoIt(task, userid)来检查登录的用户(将具有特定的 groupTitle)是否可以执行(函数返回 true 或 false 对于提供的任务,简而言之)

Suppose, I want to check whether a logged in user can perform the task or not, check via canDoit(task, userid), and If he cannot, the button will be disabled otherwise won't.... OnForm_Load I throw the function (or may be another time when I need it) and check for each button, ie假设,我想检查一个登录的用户是否可以执行任务,通过 canDoit(task, userid) 检查,如果他不能,按钮将被禁用,否则不会...... OnForm_Load 我抛出函数(或者可能是我需要它的另一个时间)并检查每个按钮,即

btnEdit.Enabled = canDo("canEdit", userID)
btnDelete.Enabled = canDo("canDelete", userID)
btnCreate.Enabled = canDo("canCreat", userID)

cnxMenuEdit.Enabled = canDo("canEdit", userID)
cnxMenuDelete.Enabled = canDo("canDelete", userID)
.
.
.
.....and so on and so forth.

My method work fine and good but I have doubts and questions.我的方法工作得很好,但我有疑问和问题。

First question, is good to be so?第一个问题,这样好吗?

Second question, is it professional?第二个问题,专业吗?

Another is, will that effect program or database performance?另一个是,这会影响程序或数据库的性能吗?

You need to keep your logged in user's access rights in memory.您需要将登录用户的访问权限保留在内存中。 Create some user model that will contains list of access right, load it to some static object when user logging in and then you can check access when you need.创建一些包含访问权限列表的用户模型,在用户登录时将其加载到某个静态对象,然后您可以在需要时检查访问权限。

Something like that类似的东西

public class UserModel 
{
    public List<UserAccessRight> AccessRights {get; set;}
}

public class UserAccessRight
{
    public string Name {get; set;}
}

public static class SomeAuthHelperClass
{
    public UserModel CurrentUser {get; set;}

    // some helper methods to retrieve data etc.

   public static CanDo(string accessRight)
   {
       return CurrentUser.AccessRights.Contains(ar => ar.Name.Equals(accessRight);
   }
}
.....

btnEdit.Enabled = SomeAuthHelperClass.CanDo("EditSomething");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM