布尔型、枚举型、标志型
我正在开发一个允许用户登录的网站。所有用户有1个账户,一个账户可以有多个订单。用户可以是客户、工人或经理。客户从来不是工人,也不是经理。经理也是工人。我想根据登录用户的类型呈现页面/导航选项的其他部分。
目前,我正在使用一组布尔值将用户标记为特定类型,测试该值,并运行一些 if/ elsif 阻止生成用户看到的页面。
class User
include DataMapper::Resource
# ...
# various properties
# ...
property :client, Boolean
property :worker, Boolean
property :manager, Boolean
end
然后我使用 before 过滤器来测试用户类型并将结果设置为变量。
before do
@user = session[:user]
if @user.client?
@ura = 'client'
elsif @user.worker?
@ura = 'worker'
elseif @user.manager?
@ura = 'manager'
end
end
然后在我看来我有@ura 可以玩。在我看来,这已经给我的经理带来了问题,因为@ura 必须既是工人又是工人。经理。我可以在我的观点中使用一些或,但我认为更好的解决方案是将用户类型设置为枚举或标志。但我真的不明白如何在我的过程中使用它。
我想知道每个的优点/缺点是什么?一个基本示例,说明我如何最终在 @ura 中获得适当的值。
I am working on a website that allows users to login. All users have 1 account, an account can have many orders. A user may be a client, worker, or manager. A client is never a worker nor a manager. a manager is also a worker. I would like to present additional sections of pages/ navigation options depending on the type of user logged in.
Currently I am using a set of boolean values to mark the user as a specific type, test for that value, and run through some if/elsif blocks to generate the page that the user sees.
class User
include DataMapper::Resource
# ...
# various properties
# ...
property :client, Boolean
property :worker, Boolean
property :manager, Boolean
end
And then I am using a before filter to test for the user type and set the result as variable.
before do
@user = session[:user]
if @user.client?
@ura = 'client'
elsif @user.worker?
@ura = 'worker'
elseif @user.manager?
@ura = 'manager'
end
end
Then in my views I have @ura to play around with. It seems to me that this is already going to cause me problems for managers because @ura will have to be both worker & manager. I could use some or's in my views but I think a better solution would to have the user type set as an Enum or Flag. But I don't really understand how to use that in my process.
I would like to know what the advantages/disadvantages of each are & a basic example of how I can end up with an appropriate value in @ura.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不记得我是如何解决这个问题的(我认为这是一个自行车送货应用程序),但今天才注意到它仍然没有答案。
所以,不到十年后,即使我不再经常使用 DataMapper,他也是我现在解决这个问题的方法;
当时我还没有完全理解身份验证和身份验证之间的区别。与用户在系统中的
角色
相关的授权。这样所有用户都可以拥有他们需要的东西,员工可以进一步分为
Worker
和Worker
。经理
。然后您可以对相应的模型进行适当的验证。预先编写更多代码,但更易于维护和维护更容易扩展,只需向User
Enum & 添加新角色即可对应的类。如果该类没有做任何超出命名空间/逻辑分离的事情,您甚至可以通过一些元编程/反射从枚举和反射中推断出类。在初始化时包含适当的模块。
I don't remember how I solved this exact issue ( I think it was for a bicycle delivery app ), but today just noticed that it is still unanswered.
So, just shy of a decade later, even though I no longer use DataMapper regularly, he is how I would solve this now;
At the time I did not understand fully the difference between authentication & authorization with respect to a users's
Role
in a system.This way all users can have the things they need, employees could be further split into
Worker
&Manager
. then you can put the appropriate validations on the corresponding model. More code upfront but more maintainable & more easy to scale, just add a new role to theUser
Enum & a corresponding class.If the class doesn't do anything beyond namespace/logical separation you might even be able to get away with some meta programing/reflection infer the classes from the enum & include appropriate modules at initialization.
我不确定,但数组可能是更好的解决方案:
I'm not sure, but may be an Array will be better solution: