There are several sites in our business that we manage, and each of these sites has sites for which they are responsible, and so on. Thus, everything is hierarchical, since permissions apply to our software. If a person on site-X wants to edit material for site-X and any sub-site-X, they should be allowed. We also have application roles, mainly administrators, that will allow the person to edit everything and also support the application.
I am currently working on permission permissions for this application, and everything works for me, but I really hate it. Its awkward, not very verifiable and doesn't seem like its right place for my MVC application. I was hoping that someone would have some thoughts on how I could reorganize this code and make it the most important for testing, and possibly make it more convenient to use.
Thanks in advance.
public class OuController : BaseController {
private readonly IOrganizationUnitRepository repo;
public OUController(IOrganizationUnitRepository repo) {
this.repo = repo;
}
public ActionResult Details(string site) {
var ou = repo.GetOuByName(site);
if (ou != null) {
var roles = ((RolePrincipal)User).GetRoles().ToList();
var parents = repo.GetParents(ou, new List<OU>());
var model = new OrganizationalViewModel(ou, parents);
if (roles.Any()) {
if(roles.Contains(InfoRoles.Administrator.ToString())) {
model.CanEdit = true;
} else if(parents == null) {
model.CanEdit = roles.Contains(ou.DisplayName);
} else {
model.CanEdit = parents.Any(c => roles.Contains(c.DisplayName));
}
}
return View("Details", model);
}
return View("NotFound");
}
}
}
source
share