根据用户权限启用用户界面元素的设计模式? [英] Design patterns for enabling user interface elements based on user permissions?

查看:186
本文介绍了根据用户权限启用用户界面元素的设计模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个Web应用程序,其前端使用GWT / GXT实现。用户可以属于不同的组,然后这些组可以拥有不同的权限。权限是相当细致的,例如comment_create,comment_edit,comment_delete和comment_read。



根据用户权限启用和禁用用户界面控件的最佳方式是什么?我们有一个实用程序方法,它返回给定用户和权限名称的布尔值。但目前每个控件都包含在if子句中,这会使代码变得混乱。 我有同样的问题,这里是我的解决方案。

每个UI组件都有一个开/关状态(可见/隐藏,启用/禁用,可编辑/只读),并且状态可以绑定到一个或更多权限。
例如,如果用户具有编辑权限,则可以启用编辑按钮,否则禁用该编辑按钮。



我创建了一个绑定类来绑定UI组件授予权限。活页夹通过事件总线知道当前用户权限(所有权限),其中使用事件发送一组权限。每次收到活动时,活页夹检查权限是否存在(替代方法是为每个权限使用布尔值)并将更改应用到组件(例如启用或禁用它)。

事件总线是所有UI组件之间的唯一连接。



使用Gin和一些辅助类我最终得到了类似这对于绑定代码:

  FeatureBinder.bind(editButton,EDIT_PERMISSION); 


We have a web application and its front end is implemented with GWT/GXT. Users can belong to various groups and these groups can then have various permissions. The permissions are quite fine grained, for example comment_create, comment_edit, comment_delete and comment_read.

What would be the best way to enable and disable user interface controls based on user permissions? We have an utility method that returns boolean value with given user and permission name. But at the moment each control is wrapped inside if clause and that makes the code bit messy.

解决方案

I had the same problem, here my solution.

Each UI component has an on/off state (visible/hidden, enabled/disabled, editable/readonly) and the state can be bound to one or more permission. For example the edit button can be enabled if the user has an EDIT permission, or disabled otherwise.

I've created a binder class that binds the UI component to a permission. The binder knows the current user permissions (all the permissions) through a event bus where a set of permissions is sent using an event. Each time the event is received the binder check if the permission is present or not (an alternative is use a boolean for each permission) and apply the changes to the component (for example enabling or disabling it).

The event bus is the only connection between all the UI components.

Using Gin and some helper class I've ended up with something like this for the binding code:

FeatureBinder.bind(editButton, EDIT_PERMISSION);

这篇关于根据用户权限启用用户界面元素的设计模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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