Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Web.Security;
- using Altairis.Nemesis.Events.Data;
- namespace Altairis.Nemesis.Events.WebCore.Security {
- public class NemesisEventsRoleProvider : RoleProvider {
- // This property is implemented for compatibility, but ignored
- public override string ApplicationName { get; set; }
- // These methods are implemented
- public override string[] GetAllRoles() {
- return new string[] { "organizers", "administrators" };
- }
- public override bool RoleExists(string roleName) {
- return this.GetAllRoles().Contains(roleName);
- }
- public override string[] FindUsersInRole(string roleName, string usernameToMatch) {
- if (roleName == null) throw new ArgumentNullException("roleName");
- if (string.IsNullOrWhiteSpace(roleName)) throw new ArgumentException("Value cannot be empty or whitespace only string.", "roleName");
- if (usernameToMatch == null) throw new ArgumentNullException("usernameToMatch");
- if (string.IsNullOrWhiteSpace(usernameToMatch)) throw new ArgumentException("Value cannot be empty or whitespace only string.", "usernameToMatch");
- roleName = roleName.ToLower().Trim();
- usernameToMatch = usernameToMatch.ToLower().Trim();
- using (var dc = new NemesisEventsEntities()) {
- switch (roleName) {
- case "organizers":
- var org = from u in dc.Users
- where u.UserName.Contains(usernameToMatch) & u.IsOrganizer
- orderby u.UserName
- select u.UserName;
- return org.ToArray();
- case "administrators":
- var adm = from u in dc.Users
- where u.UserName.Contains(usernameToMatch) & u.IsAdministrator
- orderby u.UserName
- select u.UserName;
- return adm.ToArray();
- default:
- return new string[0];
- }
- }
- }
- public override string[] GetRolesForUser(string username) {
- if (username == null) throw new ArgumentNullException("username");
- if (string.IsNullOrWhiteSpace(username)) throw new ArgumentException("Value cannot be empty or whitespace only string.", "username");
- using (var dc = new NemesisEventsEntities()) {
- var user = dc.Users.SingleOrDefault(u => u.UserName.Equals(username));
- if (user != null) {
- if (user.IsAdministrator && user.IsOrganizer) { // admin & orga
- return new string[] { "organizers", "administrators" };
- }
- else if (user.IsAdministrator) { // admin only
- return new string[] { "organizers" };
- }
- else if (user.IsOrganizer) { // orga only
- return new string[] { "administrators" };
- }
- }
- // Regular user or not found
- return new string[0];
- }
- }
- public override string[] GetUsersInRole(string roleName) {
- if (roleName == null) throw new ArgumentNullException("roleName");
- if (string.IsNullOrWhiteSpace(roleName)) throw new ArgumentException("Value cannot be empty or whitespace only string.", "roleName");
- roleName = roleName.ToLower().Trim();
- using (var dc = new NemesisEventsEntities()) {
- switch (roleName) {
- case "organizers":
- var org = from u in dc.Users
- where u.IsOrganizer
- orderby u.UserName
- select u.UserName;
- return org.ToArray();
- case "administrators":
- var adm = from u in dc.Users
- where u.IsAdministrator
- orderby u.UserName
- select u.UserName;
- return adm.ToArray();
- default:
- return new string[0];
- }
- }
- }
- public override bool IsUserInRole(string username, string roleName) {
- if (username == null) throw new ArgumentNullException("username");
- if (string.IsNullOrWhiteSpace(username)) throw new ArgumentException("Value cannot be empty or whitespace only string.", "username");
- if (roleName == null) throw new ArgumentNullException("roleName");
- if (string.IsNullOrWhiteSpace(roleName)) throw new ArgumentException("Value cannot be empty or whitespace only string.", "roleName");
- username = username.ToLower().Trim();
- roleName = roleName.ToLower().Trim();
- using (var dc = new NemesisEventsEntities()) {
- switch (roleName) {
- case "organizers":
- return dc.Users.Any(u => u.UserName.Equals(username) & u.IsOrganizer);
- case "administrators":
- return dc.Users.Any(u => u.UserName.Equals(username) & u.IsAdministrator);
- default:
- return false;
- }
- }
- }
- // This provider is read only, so the following methods are not implemented
- public override void AddUsersToRoles(string[] usernames, string[] roleNames) {
- throw new NotImplementedException();
- }
- public override void CreateRole(string roleName) {
- throw new NotImplementedException();
- }
- public override bool DeleteRole(string roleName, bool throwOnPopulatedRole) {
- throw new NotImplementedException();
- }
- public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames) {
- throw new NotImplementedException();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement