Advertisement
altair

GOC335 NemesisEventsRoleProvider.cs

Apr 3rd, 2011
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.43 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Web.Security;
  4. using Altairis.Nemesis.Events.Data;
  5.  
  6. namespace Altairis.Nemesis.Events.WebCore.Security {
  7.   public class NemesisEventsRoleProvider : RoleProvider {
  8.  
  9.     // This property is implemented for compatibility, but ignored
  10.  
  11.     public override string ApplicationName { get; set; }
  12.  
  13.     // These methods are implemented
  14.  
  15.     public override string[] GetAllRoles() {
  16.       return new string[] { "organizers", "administrators" };
  17.     }
  18.  
  19.     public override bool RoleExists(string roleName) {
  20.       return this.GetAllRoles().Contains(roleName);
  21.     }
  22.  
  23.     public override string[] FindUsersInRole(string roleName, string usernameToMatch) {
  24.       if (roleName == null) throw new ArgumentNullException("roleName");
  25.       if (string.IsNullOrWhiteSpace(roleName)) throw new ArgumentException("Value cannot be empty or whitespace only string.", "roleName");
  26.       if (usernameToMatch == null) throw new ArgumentNullException("usernameToMatch");
  27.       if (string.IsNullOrWhiteSpace(usernameToMatch)) throw new ArgumentException("Value cannot be empty or whitespace only string.", "usernameToMatch");
  28.  
  29.       roleName = roleName.ToLower().Trim();
  30.       usernameToMatch = usernameToMatch.ToLower().Trim();
  31.  
  32.       using (var dc = new NemesisEventsEntities()) {
  33.         switch (roleName) {
  34.           case "organizers":
  35.             var org = from u in dc.Users
  36.                       where u.UserName.Contains(usernameToMatch) & u.IsOrganizer
  37.                       orderby u.UserName
  38.                       select u.UserName;
  39.             return org.ToArray();
  40.           case "administrators":
  41.             var adm = from u in dc.Users
  42.                       where u.UserName.Contains(usernameToMatch) & u.IsAdministrator
  43.                       orderby u.UserName
  44.                       select u.UserName;
  45.             return adm.ToArray();
  46.           default:
  47.             return new string[0];
  48.         }
  49.       }
  50.     }
  51.  
  52.     public override string[] GetRolesForUser(string username) {
  53.       if (username == null) throw new ArgumentNullException("username");
  54.       if (string.IsNullOrWhiteSpace(username)) throw new ArgumentException("Value cannot be empty or whitespace only string.", "username");
  55.  
  56.       using (var dc = new NemesisEventsEntities()) {
  57.         var user = dc.Users.SingleOrDefault(u => u.UserName.Equals(username));
  58.         if (user != null) {
  59.           if (user.IsAdministrator && user.IsOrganizer) { // admin & orga
  60.             return new string[] { "organizers", "administrators" };
  61.           }
  62.           else if (user.IsAdministrator) {                // admin only
  63.             return new string[] { "organizers" };
  64.           }
  65.           else if (user.IsOrganizer) {                    // orga only
  66.             return new string[] { "administrators" };
  67.           }
  68.         }
  69.  
  70.         // Regular user or not found
  71.         return new string[0];
  72.  
  73.       }
  74.     }
  75.  
  76.     public override string[] GetUsersInRole(string roleName) {
  77.       if (roleName == null) throw new ArgumentNullException("roleName");
  78.       if (string.IsNullOrWhiteSpace(roleName)) throw new ArgumentException("Value cannot be empty or whitespace only string.", "roleName");
  79.  
  80.       roleName = roleName.ToLower().Trim();
  81.  
  82.       using (var dc = new NemesisEventsEntities()) {
  83.         switch (roleName) {
  84.           case "organizers":
  85.             var org = from u in dc.Users
  86.                       where u.IsOrganizer
  87.                       orderby u.UserName
  88.                       select u.UserName;
  89.             return org.ToArray();
  90.           case "administrators":
  91.             var adm = from u in dc.Users
  92.                       where u.IsAdministrator
  93.                       orderby u.UserName
  94.                       select u.UserName;
  95.             return adm.ToArray();
  96.           default:
  97.             return new string[0];
  98.         }
  99.       }
  100.     }
  101.  
  102.     public override bool IsUserInRole(string username, string roleName) {
  103.       if (username == null) throw new ArgumentNullException("username");
  104.       if (string.IsNullOrWhiteSpace(username)) throw new ArgumentException("Value cannot be empty or whitespace only string.", "username");
  105.       if (roleName == null) throw new ArgumentNullException("roleName");
  106.       if (string.IsNullOrWhiteSpace(roleName)) throw new ArgumentException("Value cannot be empty or whitespace only string.", "roleName");
  107.  
  108.       username = username.ToLower().Trim();
  109.       roleName = roleName.ToLower().Trim();
  110.  
  111.       using (var dc = new NemesisEventsEntities()) {
  112.         switch (roleName) {
  113.           case "organizers":
  114.             return dc.Users.Any(u => u.UserName.Equals(username) & u.IsOrganizer);
  115.           case "administrators":
  116.             return dc.Users.Any(u => u.UserName.Equals(username) & u.IsAdministrator);
  117.           default:
  118.             return false;
  119.         }
  120.       }
  121.     }
  122.  
  123.     // This provider is read only, so the following methods are not implemented
  124.  
  125.     public override void AddUsersToRoles(string[] usernames, string[] roleNames) {
  126.       throw new NotImplementedException();
  127.     }
  128.  
  129.     public override void CreateRole(string roleName) {
  130.       throw new NotImplementedException();
  131.     }
  132.  
  133.     public override bool DeleteRole(string roleName, bool throwOnPopulatedRole) {
  134.       throw new NotImplementedException();
  135.     }
  136.  
  137.     public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames) {
  138.       throw new NotImplementedException();
  139.     }
  140.  
  141.   }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement