asp.net mvc - How can I check more than one condition on a logged in user? For instance, user is the author of the article or belongs to a certain role -


i have portion of page i'll display if user logged in satisfies 1 of 3 following conditions:

  1. he's author
  2. he belongs 'editors' role.
  3. he belongs 'admin' role.

the object model contains property called addedby (model.addedby)

how can check 3 conditions on asp.net mvc page.

so far i've tried below code

<%if((page.user.identity.name == model.addedby) ||     (page.user.isinrole("admin")) ||     (page.user.isinrole("editors"))){%>     //display portion of page...   <%}%> 

but code doesn't seem notice difference , keeps displaying portion of html enclosed inside above code.

i must wrong somewhere.

edit

the html portion enclosed in code contains buttons such delete article, edit article, add article, allow comments, ... author of article supposed see buttons or check boxes. except users in editors role or admin role can manipulate articles don't own.

that way, user accessing page can see portion if (i) the author or (ii) belongs editors role or (iii) admin role.

thanks helping

edit: after clarification of question op seems or condition should being used, , problem solved old traditional debugging! hurrah! below left comments make sense, not answer modified question. might still useful sometime though...

is because going if condition if any of conditions satisfied (you using || or) , not all of conditions (you want use && and), question implies want?

i think should give want:

<%if((page.user.identity.name == model.addedby) &&     (page.user.isinrole("admin")) &&     (page.user.isinrole("editors"))){%>     //display portion of page...   <%}%> 

using single & work think, using double && more efficient not try evaluate subsequent conditions if 1 found false (and make sense put condition false first check in if statement)

this documentation should useful: && operator , || operator


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -