Example #1
0
 public override string[] GetAllRoles()
 {
     using (EfDbContext Context = new EfDbContext())
     {
         return Context.Roles.Select(Rl => Rl.RoleName).ToArray();
     }
 }
Example #2
0
 public override bool IsUserInRole(string username, string roleName)
 {
     if (string.IsNullOrEmpty(username))
     {
         return false;
     }
     if (string.IsNullOrEmpty(roleName))
     {
         return false;
     }
     using (EfDbContext Context = new EfDbContext())
     {
         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);
     }
 }
Example #3
0
 public override string[] GetUsersInRole(string roleName)
 {
     if (string.IsNullOrEmpty(roleName))
     {
         return null;
     }
     using (EfDbContext Context = new EfDbContext())
     {
         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;
         }
     }
 }
Example #4
0
 public override bool RoleExists(string roleName)
 {
     if (string.IsNullOrEmpty(roleName))
     {
         return false;
     }
     using (EfDbContext Context = new EfDbContext())
     {
         Role Role = null;
         Role = Context.Roles.FirstOrDefault(Rl => Rl.RoleName == roleName);
         if (Role != null)
         {
             return true;
         }
         else
         {
             return false;
         }
     }
 }
Example #5
0
 public override void AddUsersToRoles(string[] usernames, string[] roleNames)
 {
     using (EfDbContext Context = new EfDbContext())
     {
         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();
     }
 }
Example #6
0
 public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
 {
     using (EfDbContext Context = new EfDbContext())
     {
         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();
     }
 }
Example #7
0
 public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
 {
     if (string.IsNullOrEmpty(roleName))
     {
         return false;
     }
     using (EfDbContext Context = new EfDbContext())
     {
         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;
     }
 }
Example #8
0
 public override void CreateRole(string roleName)
 {
     if (!string.IsNullOrEmpty(roleName))
     {
         using (EfDbContext Context = new EfDbContext())
         {
             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();
             }
         }
     }
 }
Example #9
0
        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            if (string.IsNullOrEmpty(roleName))
            {
                return null;
            }

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

            using (EfDbContext Context = new EfDbContext())
            {

                return (from Rl in Context.Roles from Usr in Rl.Users where Rl.RoleName == roleName && Usr.Username.Contains(usernameToMatch) select Usr.Username).ToArray();
            }
        }
Example #10
0
 public override string[] GetRolesForUser(string username)
 {
     if (string.IsNullOrEmpty(username))
     {
         return null;
     }
     using (EfDbContext Context = new EfDbContext())
     {
         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;
         }
     }
 }
Example #11
0
 public MembershipController(EfDbContext efDbContext)
 {
     efDbContext = new EfDbContext();
 }