c# - How to handle different category of logged in user in asp.met mvc application -
i in learning phase of asp.net mvc 5 web development , stuck @ 1 place. so, have website have 3 flow.
- non loggedin user
- authenticated user (tenants)
- authenticated user (owner)
now during registration have added checkbox capture if user going tenant or owner. now view file 3 (non loggedin, owner , tenant) needs different.
as of know how handle two type of user (non-logged in , (tenant)logged in)
. in _viewstart.cshtml
file did below
@{ if(user.identity.isauthenticated) { layout = "~/views/shared/_loggedinlayout.cshtml"; } else { layout = "~/views/shared/_layout.cshtml"; } }
so far good. can see there no identificaiton of "tenant , owner user". given fact have property during registration true or false tenant , owner.
i want below
@{ if(user.identity.isauthenticated) { if(user.istenant) {layout = "~/views/shared/_loggedtenantlayout.cshtml"; } else{layout = "~/views/shared/_loggedinownerlayout.cshtml"; } } else { layout = "~/views/shared/_layout.cshtml"; } }
but read areas, , route. so, confused, if have 3 type of user flow, method have followed. mean need route manipulation or something. can please guide me here. know there called roles too. think adding privilege in page adding may edit button or something. beginner, can 1 tell me how can have tenant,owner , non logged in user flow separated out.
owner needs see totally different view file , options , same goes tenant. know question kind of broad if can give me basic small code snippet on how separate out flow, can take forward there.
your path forward depends on answer question: tenants , owners distinct user types or permission sets? people conflate these two. stands, looks if you're looking treat "tenants" 1 way , "owners" another, can solved via roles. essentially, tenant gets assigned role "tenant" , owner gets assigned role "owner". then, can protect controllers/actions 1 role or other via:
[authorize(roles = "owner")]
which allow owners access that. including different layout. can branch on user.isinrole
:
if (user.isinrole("tenant")) { layout = "~/views/shared/_loggedtenantlayout.cshtml"; } else if (user.isinrole("owner")) { layout = "~/views/shared/_loggedinownerlayout.cshtml"; } else { layout = "~/views/shared/_layout.cshtml"; }
if, however, these distinct user types, need collect , persist entirely different types of information, should handle via type inheritance. example, tenant might have monthlypayment
, whereas type of information makes no sense owner. in scenario, you'd do:
public class tenant : applicationuser { // tenant-specific properties } public class owner : applicationuser { // owner-specific properties } public class applicationuser: identityuser { // shared properties }
by default, entity framework handle type of inheritance creating single database table columns user types, along discriminator
column. discriminator
column hold class name saved, i.e. 1 of "tenant", "owner", or "applicationuser", entity framework use instantiate right type when query out users.
for part, works fine, must aware cannot have non-nullable columns on derived types. due fact other derived types or base type won't able fill column, because won't have property. can still enforce particular property required purposes of form collecting user input, via view model, though; can't enforced @ database level.
if that's deal-breaker or don't having in single table, can choose use table-per-type inheritance strategy. that, you'd have table each type, applicationuser
, tenant
, , owner
. however, table applicationuser
hold base columns , shared between user types, while derived-type tables hold columns specific unique properties. use type of inheritance strategy, add table
attribute derived types:
[table("tenants")] public class tenant : applicationuser
Comments
Post a Comment