示例#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;
 }
示例#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();
         }
     }
 }
示例#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();
     }
 }
示例#4
0
 public override string[] GetRolesForUser(string username)
 {
     using (Data.contentDataContext context = new Data.contentDataContext())
     {
         return (from user in context.Users
                 join userRole in context.UserRoles on user.Id equals userRole.UserId
                 join role in context.Roles on userRole.RoleId equals role.Id
                 where user.Name == username
                 select role.Name).ToArray();
     }
 }
示例#5
0
 public override string[] GetAllRoles()
 {
     using (Data.contentDataContext context = new Data.contentDataContext())
     {
         return context.Roles.Select(p => p.Name).ToArray();
     }
 }
示例#6
0
 public override bool RoleExists(string roleName)
 {
     using (Data.contentDataContext context = new Data.contentDataContext())
     {
         return context.Roles.Where(p => p.Name == roleName).Count() > 0;
     }
 }
示例#7
0
 public override bool IsUserInRole(string username, string roleName)
 {
     using (Data.contentDataContext context = new Data.contentDataContext())
     {
         return (from user in context.Users
                 join userRole in context.UserRoles on user.Id equals userRole.UserId
                 join role in context.Roles on userRole.RoleId equals role.Id
                 where user.Name == username && role.Name == roleName
                 select role
                 ).Count() > 0;
     }
 }
示例#8
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();
     }
 }
示例#9
0
 public override string[] GetUsersInRole(string roleName)
 {
     using (Data.contentDataContext context = new Data.contentDataContext())
     {
         return (from role in context.Roles
                 join userRole in context.UserRoles on role.Id equals userRole.RoleId
                 join user in context.Users on userRole.UserId equals user.Id
                 where role.Name == roleName
                 select user.Name).ToArray();
     }
 }
示例#10
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;
         }
     }
 }
示例#11
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;
         }
     }
 }
示例#12
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;
 }
示例#13
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;
 }