asp.net mvc - Roles base on item in MVC -
i quite new programming , english quite bad sorry if post confused you.
i implementing basic asp .net mvc web application manage information of companies. there 2 pages in application. 1 viewing information, not require access page. 1 edit information can access page if have admin role of company.
public actionresult viewinfo(string companyid) { return view(); } [authorize(roles = "admin")] public actionresult editinfo(string companyid) { return view(); } if have account can access 2 companies account has different roles on both of companies. (e.g. account admin of company , normal user of company b).
is there easy way role base on company going access to? i've tried using custom roleprovider seems cannot pass additional parameters method.
somewhat this?
public class customroleprovider : roleprovider { public override bool isuserinrole(string username, string rolename) { string companyid = getcompanyid(); // there way companyid controller? return usercompanyroles.any(u => u.username == username && u.companyid == companyid && u.roles.any(r => r.name == rolename)); } ... } edit
there 4 models in application:
public class user { public string id { get; set;} ... } public class company { public string id { get; set;} ... } public class role { public string name { get; set;} ... } public class usercompanyroles { public string userid { get; set; } public string companyid { get; set; } public list<string> roles { get; set; } } so table this
userid companyid role 1 user 1 admin 1 b user 2 b admin 2 c user 3 admin 3 c admin
the membership provider controls roles each user has. on user user basis, rather on user/company. doing right thing using [authorize(roles = "admin")]
however sounds want check if current user has access current company. bit this.
[authorize(roles = "admin")] public actionresult editinfo(string companyid) { if (userhasaccesstocompany(companyid)) { return view(); } return redirecttoaction("index"); } private bool userhasaccesstocompany(string companyid) { var company = companyrepository.getcompanybyid(companyid); // making assumption have repository companies return company.users.select(u => u.userid).include(user.identity.name); // can access current user using `user` within controller } assuming might want check if user has access lot function moved basecontroller can access everywhere.
Comments
Post a Comment