Beispiel #1
0
 public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
 {
     using (Data.contentDataContext context = new Data.contentDataContext())
     {
         var role = context.Roles.Where(p => p.Name == roleName).SingleOrDefault();
         if (role != null)
         {
             context.Roles.DeleteOnSubmit(role);
             context.SubmitChanges();
             return true;
         }
     }
     return false;
 }
Beispiel #2
0
 public override void CreateRole(string roleName)
 {
     using (Data.contentDataContext context = new Data.contentDataContext())
     {
         if (context.Roles.Where(p => p.Name == roleName).Count() <= 0)
         {
             Data.Role role = new Data.Role()
             {
                 Name = roleName
             };
             context.Roles.InsertOnSubmit(role);
             context.SubmitChanges();
         }
     }
 }
Beispiel #3
0
 public override void AddUsersToRoles(string[] usernames, string[] roleNames)
 {
     using (Data.contentDataContext context = new Data.contentDataContext())
     {
         var list = from user in context.Users
                    join name in usernames on user.Name equals name
                    from role in context.Roles
                    join roleName in roleNames on role.Name equals roleName
                    select new Data.UserRole
                    {
                        UserId = user.Id,
                        RoleId = role.Id
                    };
         context.UserRoles.InsertAllOnSubmit(list);
         context.SubmitChanges();
     }
 }
Beispiel #4
0
 public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
 {
     using (Data.contentDataContext context = new Data.contentDataContext())
     {
         var list = from user in context.Users
                    join name in usernames on user.Name equals name
                    join userRole in context.UserRoles on user.Id equals userRole.UserId
                    join role in context.Roles on userRole.RoleId equals role.Id
                    join roleName in roleNames on role.Name equals roleName
                    select userRole;
         context.UserRoles.DeleteAllOnSubmit(list);
         context.SubmitChanges();
     }
 }
Beispiel #5
0
 public override bool ChangePassword(string username, string oldPassword, string newPassword)
 {
     using (Data.contentDataContext context = new Data.contentDataContext())
     {
         var user = context.Users.Where(p => p.Name == username).SingleOrDefault();
         if (user == null)
         {
             return false;
         }
         else
         {
             bool valid = Hash.ValidatePassword(oldPassword, user.Password);
             if (valid)
             {
                 user.Password = Hash.HashPassword(newPassword);
                 user.LastModified = DateTime.Now;
                 context.SubmitChanges();
             }
             return valid;
         }
     }
 }
Beispiel #6
0
 public override bool ValidateUser(string username, string password)
 {
     using (Data.contentDataContext context = new Data.contentDataContext())
     {
         var user = context.Users.Where(p => p.Name == username).SingleOrDefault();
         if (user == null)
         {
             return false;
         }
         else
         {
             bool valid = Hash.ValidatePassword(password, user.Password);
             if (valid)
             {
                 user.LastAccess = DateTime.Now;
                 context.SubmitChanges();
             }
             var cabUser = GetUser(user);
             HttpContext.Current.Cache.Add(cabUser.UserName, cabUser, null,
                 System.Web.Caching.Cache.NoAbsoluteExpiration, FormsAuthentication.Timeout,
                 System.Web.Caching.CacheItemPriority.Default, null);
             return valid;
         }
     }
 }
Beispiel #7
0
 public override bool DeleteUser(string username, bool deleteAllRelatedData)
 {
     using (Data.contentDataContext context = new Data.contentDataContext())
     {
         var user = context.Users.Where(p => p.Name == username).SingleOrDefault();
         if (user != null)
         {
             //Deletes the record. We may consider in the future to disable as per deleteAllRelatedData
             context.Users.DeleteOnSubmit(user);
             context.SubmitChanges();
             return true;
         }
     }
     return false;
 }
Beispiel #8
0
 public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
 {
     using (Data.contentDataContext context = new Data.contentDataContext())
     {
         try
         {
             if (context.Users.Where(p => p.Name == username).Count() > 0)
             {
                 status = MembershipCreateStatus.DuplicateUserName;
                 return null;
             }
             Data.User user = new Data.User()
             {
                 Name = username,
                 Created = DateTime.Now,
                 LastModified = DateTime.Now,
                 Email = email,
                 Password = Hash.HashPassword(password)
             };
             //TODO: Checks before inserting
             context.Users.InsertOnSubmit(user);
             context.SubmitChanges();
             status = MembershipCreateStatus.Success;
             var cabUser = GetUser(user);
             HttpContext.Current.Cache.Add(cabUser.UserName, cabUser, null,
                 System.Web.Caching.Cache.NoAbsoluteExpiration, FormsAuthentication.Timeout,
                 System.Web.Caching.CacheItemPriority.Default, null);
             return cabUser;
         }
         catch (Exception ex)
         {
             Elmah.ErrorLog.GetDefault(null).Log(new Elmah.Error(ex));
             status = MembershipCreateStatus.ProviderError;
         }
     }
     return null;
 }