如何解决具有分层组织角色的应用程序角色
| 我们的业务有许多我们管理的站点,每个站点都有他们负责的站点,依此类推。因此,就我们软件的权限而言,一切都是分层的。如果站点X的人员想要编辑站点X和任何子站点X的内容,则应允许他们进行编辑。我们还具有应用程序角色,主要是管理员,该角色允许用户编辑所有内容并维护应用程序。 我目前正在处理此应用程序的权限,并且一切正常,但我真的很讨厌它。它笨拙,测试性不强,似乎不适合我的MVC应用程序。我希望有人会对如何重构此代码并使之最重要地使其更具可测试性,甚至使其更加实用而有所思考。 先感谢您。 public class OuController : BaseController {
private readonly IOrganizationUnitRepository repo;
public OUController(IOrganizationUnitRepository repo) {
this.repo = repo;
}
public ActionResult Details(string site) {
//Get the site we are viewing
var ou = repo.GetouByName(site);
//make sure the site really exists
if (ou != null) {
//Get all the roles for the current user via the role provider
//will return the sites they are able to manage along with
//any application roles they have
var roles = ((RolePrincipal)User).GetRoles().ToList();
//Get all the parents of the current ou,this will include itself
var parents = repo.GetParents(ou,new List<OU>());
//create a new viewmodel object
//ou is used for details obvIoUsly
//parents are used for a breadcrumb
var model = new Organizationalviewmodel(ou,parents);
//if a user has no roles,there is no way he can possibly edit
if (roles.Any()) {
if(roles.Contains(InfoRoles.Administrator.ToString())) {
model.CanEdit = true;
} else if(parents == null) {
//If there are no parents,check if this ou is in users list of roles
model.CanEdit = roles.Contains(ou.displayName);
} else {
//check to see if any of the roles i have are parents of the current ou
model.CanEdit = parents.Any(c => roles.Contains(c.displayName));
}
}
return View(\"Details\",model);
}
return View(\"NotFound\");
}
}
}
解决方法
任何看起来像这样的东西:
((RolePrincipal)User).GetRoles().ToList()
...属于其自己的类(具有\“ GetCurrentRoles \”这样的接口方法),因此可以轻松对其进行模拟。
此外,这:
//if a user has no roles,there is no way he can possibly edit
if (roles.Any()) {
if(roles.Contains(InfoRoles.Administrator.ToString())) {
return true;
} else if(parents == null) {
//If there are no parents,check if this ou is in users list of roles
return roles.Contains(ou.DisplayName);
} else {
//check to see if any of the roles i have are parents of the current ou
return parents.Any(c => roles.Contains(c.DisplayName));
}
...属于一种称为in3 called的方法的实用程序类。这样,您的控制器就可以说:
var roles = _sessionManager.GetCurrentRoles();
...
model.Edit = _orgViewRightsUtil.CanRolesEditOrganizationalView(roles,...);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。