예제 #1
0
 public static ClubPoolPrincipal CreateUnauthorizedPrincipal()
 {
     var principal = new ClubPoolPrincipal();
       principal.Identity = new GenericIdentity("");
       principal.roles = new string[0];
       return principal;
 }
예제 #2
0
        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {
            // We need to set the HttpContext.User property to our own IPrincipal implementation
              // so that it uses our role service to satisfy IPrincipal.IsInRole(). The
              // Authorize attribute uses this method to determine whether the authenticated user
              // is in the requested role

              // Extract the forms authentication cookie
              string cookieName = FormsAuthentication.FormsCookieName;
              HttpCookie authCookie = Context.Request.Cookies[cookieName];

              if (null == authCookie) {
            // There is no authentication cookie.
            SetUnauthorizedPrincipal();
            return;
              }

              FormsAuthenticationTicket authTicket = null;
              try {
            authTicket = FormsAuthentication.Decrypt(authCookie.Value);
              }
              catch (Exception ex) {
            SetUnauthorizedPrincipal();
            return;
              }

              if (null == authTicket) {
            // Cookie failed to decrypt.
            SetUnauthorizedPrincipal();
            return;
              }

              // Create an Identity object
              FormsIdentity identity = new FormsIdentity(authTicket);

              var repository = DependencyResolver.Current.GetService<IRepository>();
              var user = repository.All<User>().Single(u => u.Username.Equals(identity.Name));
              // This principal will flow throughout the request.
              ClubPoolPrincipal principal = new ClubPoolPrincipal(user, identity);
              // Attach the new principal object to the current HttpContext object
              Context.User = principal;
        }
예제 #3
0
 private EditViewModel BuildEditViewModel(User user, ClubPoolPrincipal currentPrincipal)
 {
     var model = new EditViewModel {
     Id = user.Id,
     FirstName = user.FirstName,
     LastName = user.LastName,
     Email = user.Email,
     IsApproved = user.IsApproved,
     IsLocked = user.IsLocked,
     Username = user.Username,
     Version = user.EncodedVersion,
     Roles = user.Roles.Select(r => r.Id).ToArray()
       };
       model.ShowStatus = CanEditUserStatus(currentPrincipal, user);
       model.ShowRoles = CanEditUserRoles(currentPrincipal, user);
       if (model.ShowRoles) {
     model.AvailableRoles = repository.All<Role>().Select(r => new RoleViewModel { Id = r.Id, Name = r.Name }).ToList();
       }
       model.ShowPassword = CanEditUserPassword(currentPrincipal, user);
       return model;
 }
예제 #4
0
 protected bool CanEditUserStatus(ClubPoolPrincipal principal, User user)
 {
     // admins can edit the status of all users, officers can edit the status of
       // all other non admins but not themselves, normal users can not edit status
       var editorIsAdmin = principal.IsInRole(Roles.Administrators);
       if (editorIsAdmin) {
     // admins can edit the status of all other users
     return true;
       }
       var editorIsOfficer = principal.IsInRole(Roles.Officers);
       if (!editorIsOfficer) {
     // if the user is neither admin nor officer, can't edit any status
     return false;
       }
       // if we get here, editor is officer, can edit status of other non-admin
       // users but not self
       var editingSelf = user.Id == principal.UserId;
       var userIsAdmin = user.Roles.Where(r => r.Name.Equals(Roles.Administrators)).Any();
       return (!(editingSelf || userIsAdmin));
 }
예제 #5
0
 protected bool CanEditUserRoles(ClubPoolPrincipal principal, User user)
 {
     // only admins can edit roles
       return principal.IsInRole(Roles.Administrators);
 }
예제 #6
0
 protected bool CanEditUserPassword(ClubPoolPrincipal principal, User user)
 {
     // admins & self can edit password
       return principal.IsInRole(Roles.Administrators) || principal.UserId == user.Id;
 }
예제 #7
0
 protected bool CanEditUser(ClubPoolPrincipal principal, User user)
 {
     // admins & officers can edit the basic properties of all users,
       // normal users can edit their own basic properties
       var editorIsAdmin = principal.IsInRole(Roles.Administrators);
       var editorIsOfficer = principal.IsInRole(Roles.Officers);
       var editingSelf = user.Id == principal.UserId;
       return editingSelf || editorIsOfficer || editorIsAdmin;
 }