Пример #1
0
 public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
 {
     if (string.IsNullOrEmpty(roleName))
     {
         return false;
     }
     using (DataContext Context = new DataContext())
     {
         Role Role = null;
         Role = Context.Roles.FirstOrDefault(Rl => Rl.RoleName == roleName);
         if (Role == null)
         {
             return false;
         }
         if (throwOnPopulatedRole)
         {
             if (Role.Users.Any())
             {
                 return false;
             }
         }
         else
         {
             Role.Users.Clear();
         }
         Context.Roles.Remove(Role);
         Context.SaveChanges();
         return true;
     }
 }
Пример #2
0
 public override void AddUsersToRoles(string[] usernames, string[] roleNames)
 {
     using (DataContext Context = new DataContext())
     {
         List<User> Users = Context.Users.Where(Usr => usernames.Contains(Usr.Username)).ToList();
         List<Role> Roles = Context.Roles.Where(Rl => roleNames.Contains(Rl.RoleName)).ToList();
         foreach (User user in Users)
         {
             foreach (Role role in Roles)
             {
                 if (!user.Roles.Contains(role))
                 {
                     user.Roles.Add(role);
                 }
             }
         }
         Context.SaveChanges();
     }
 }
Пример #3
0
 public override void CreateRole(string roleName)
 {
     if (!string.IsNullOrEmpty(roleName))
     {
         using (DataContext Context = new DataContext())
         {
             Role Role = null;
             Role = Context.Roles.FirstOrDefault(Rl => Rl.RoleName == roleName);
             if (Role == null)
             {
                 Role NewRole = new Role
                 {
                     RoleId = Guid.NewGuid(),
                     RoleName = roleName
                 };
                 Context.Roles.Add(NewRole);
                 Context.SaveChanges();
             }
         }
     }
 }
Пример #4
0
 public Repository()
 {
     datacontext = new DataContext();
 }
Пример #5
0
 public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
 {
     using (DataContext Context = new DataContext())
     {
         foreach (String username in usernames)
         {
             String us = username;
             User user = Context.Users.FirstOrDefault(U => U.Username == us);
             if (user != null)
             {
                 foreach (String roleName in roleNames)
                 {
                     String rl = roleName;
                     Role role = user.Roles.FirstOrDefault(R => R.RoleName == rl);
                     if (role != null)
                     {
                         user.Roles.Remove(role);
                     }
                 }
             }
         }
         Context.SaveChanges();
     }
 }
Пример #6
0
 public override bool RoleExists(string roleName)
 {
     if (string.IsNullOrEmpty(roleName))
     {
         return false;
     }
     using (DataContext Context = new DataContext())
     {
         Role Role = null;
         Role = Context.Roles.FirstOrDefault(Rl => Rl.RoleName == roleName);
         if (Role != null)
         {
             return true;
         }
         else
         {
             return false;
         }
     }
 }
Пример #7
0
 public override bool IsUserInRole(string username, string roleName)
 {
     if (string.IsNullOrEmpty(username))
     {
         return false;
     }
     if (string.IsNullOrEmpty(roleName))
     {
         return false;
     }
     using (DataContext Context = new DataContext())
     {
         User User = null;
         User = Context.Users.FirstOrDefault(Usr => Usr.Username == username);
         if (User == null)
         {
             return false;
         }
         Role Role = Context.Roles.FirstOrDefault(Rl => Rl.RoleName == roleName);
         if (Role == null)
         {
             return false;
         }
         return User.Roles.Contains(Role);
     }
 }
Пример #8
0
 public override string[] GetUsersInRole(string roleName)
 {
     if (string.IsNullOrEmpty(roleName))
     {
         return null;
     }
     using (DataContext Context = new DataContext())
     {
         Role Role = null;
         Role = Context.Roles.FirstOrDefault(Rl => Rl.RoleName == roleName);
         if (Role != null)
         {
             return Role.Users.Select(Usr => Usr.Username).ToArray();
         }
         else
         {
             return null;
         }
     }
 }
Пример #9
0
 public override string[] GetRolesForUser(string username)
 {
     if (string.IsNullOrEmpty(username))
     {
         return null;
     }
     using (DataContext Context = new DataContext())
     {
         User User = null;
         User = Context.Users.FirstOrDefault(Usr => Usr.Username == username);
         if (User != null)
         {
             return User.Roles.Select(Rl => Rl.RoleName).ToArray();
         }
         else
         {
             return null;
         }
     }
 }
Пример #10
0
 public override string[] GetAllRoles()
 {
     using (DataContext Context = new DataContext())
     {
         return Context.Roles.Select(Rl => Rl.RoleName).ToArray();
     }
 }
Пример #11
0
        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            if (string.IsNullOrEmpty(roleName))
            {
                return null;
            }

            if (string.IsNullOrEmpty(usernameToMatch))
            {
                return null;
            }

            using (DataContext Context = new DataContext())
            {

                return (from Rl in Context.Roles from Usr in Rl.Users where Rl.RoleName == roleName && Usr.Username.Contains(usernameToMatch) select Usr.Username).ToArray();
            }
        }