C ++中使用按位运算符的用户权限类 [英] User rights class in c++ using bitwise operators

查看:69
本文介绍了C ++中使用按位运算符的用户权限类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个设计问题.我想实现类似UserRight类的东西,它将为用户赋予不同的控件不同的特权.例如,一个人可以看到CEdit控件,而另一个则不能,或者一个人可以编辑网格中的列,而另一个则不能.
我将使用用户名和密码在数据库中定义表(其他字段将是我要讨论的一些访问级别字段).在应用程序启动时,将提示用户输入用户名和密码.如果它是有效的用户(检入数据库),则将开始应用.开始时,我将从数据库中检索访问级别信息,并创建将包含此信息的类UserRight的单个实例.然后,我将在代码中使用它来按访问级别显示或隐藏控件.我的问题是:
实现此类的最佳方法是什么?
我应该使用简单的整数来存储信息并使用按位运算符来确定访问级别吗?
是否可以将所有信息保留在表数据库的一个字段中,因为对于某些控件,我将具有两个以上的访问级别选项(例如,一个可以看到该控件并且可以进行编辑,一个只能看到而一个不能看到) ?

It is a design issue. I want to implement something like UserRight class that will give a user different privileges to different controls. For example one can see CEdit control and other can''t or one can edit column in the grid and other can''t.
I will define table in the database with users and passwords(additional fields will be some access level fields that I want to discuss). At the start of the application user will be prompted for username and password. If it is valid user(check in database) aplication will start. At start I will retrieve access level information from the database and create single instance of the class UserRight which will contain this information. Then I will use it in the code to show or hide controls by the access level. My questions are:
What is the best way to implement this class?
Should I use simple integer to store information and use bitwise operators to determine access level?
Is it possible to keep all information in one field in table database, because for some controls I will have more than two access level options(e.g. one can see the control and can edit, one can only see and one can''t see) ?
Is there any article regarding this?

推荐答案

好吧,我不知道最好的方法是什么(但没有人提供反馈),但是存储了将权限设置为整数(可以让您使用32位)不是一个坏主意,这是linux管理其文件权限的方式,就存储而言,您可能必须加密数据库以确保安全.
well, i don''t know what the best method would be (but no one else has provided feedback) but storing the permissions as an integer (which would give you 32bits to play with) is not a bad idea, that''s the way linux manages their file permissions and as far as storage, you''ll probably have to encrypt the database for security.


我会结合Mika和Albert的答案.它应该是一类可以在其中添加和删除特权的类,但位于幕后( http://en.wikipedia.org/wiki/Facade_pattern [ ^ ]),设置的位为1实现权限集概念的最佳方法.如果由于某种原因扩展的需求将使该实现失败,则可以很容易地重新实现而不更改类的接口.

—SA
I would combine the answers by Mika and Albert. It should be a class where you can add and remove privileges, but behind the facade (http://en.wikipedia.org/wiki/Facade_pattern[^]), the bit set is one of the best way to implement the notion of the set of permission. If by some reason extended requirements will fail this implementation, it will be easy to re-implement not changing the class''s interface.

—SA




我不会这样做(实际上,在没有可行的替代方案的情况下,实际上会这样做一次).相反,为什么不定义适当的类来告诉用户是否具有特权.尽管按位处理整数比处理类实例的集合要快一些,但是性能不应该成为问题.您当然可以创建一个小的测试运行,以尝试其工作方式.

不这样做的主要原因之一是可维护性.当不必使用2的幂时,更容易理解和修改数据.
Hi,

I wouldn''t do this (actually did this once when there was no feasible alternatives). Instead, why not define proper classes to tell if the user has the privilege or not. Although handling integers with bitwise is slightly faster than handling collections of class instances, the performance shouldn''t be an issue. You can of course create a small test-run to try how it works.

One of the main reasons for not doing this is the maintainability. It''s much more simpler to understand and to modify the data when not having to work with powers of 2.


这篇关于C ++中使用按位运算符的用户权限类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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