Пример #1
0
        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            SecUtility.CheckArrayParameter(ref roleNames, true, true, true, 256, "roleNames");
            SecUtility.CheckArrayParameter(ref usernames, true, true, true, 256, "usernames");

            var users = db.Users.Include("Roles").Where(c => usernames.Contains(c.UserName)).ToList();

            var roles = db.Roles.Where(c => roleNames.Contains(c.RoleName)).ToList();

            foreach (var user in users)
            {
                foreach (var role in roles)
                {
                    if (!user.Roles.Contains(role))
                    {
                        user.Roles.Add(role);
                    }
                }
            }

            db.SaveChanges();
        }
Пример #2
0
        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            SecUtility.CheckArrayParameter(ref roleNames, true, true, true, 256, "roleNames");
            SecUtility.CheckArrayParameter(ref usernames, true, true, true, 256, "usernames");


            foreach (var username in usernames)
            {
                var user = db.Users.Include("Roles").Where(u => u.UserName == username).FirstOrDefault();
                if (user != null)
                {
                    foreach (var rolename in roleNames)
                    {
                        var role = user.Roles.FirstOrDefault(r => r.RoleName == rolename);
                        if (role != null)
                        {
                            user.Roles.Remove(role);
                        }
                    }
                }
            }
            db.SaveChanges();
        }