Exemplo n.º 1
0
        public void AddRoleToGroup(int groupId, string roleName)
        {
            Group           group = _db.Groups.Find(groupId);
            ApplicationRole role  = _db.Roles.First(r => r.Name == roleName);

            var newgroupRole = new ApplicationRoleGroup
            {
                GroupId = group.Id,
                Group   = group,
                RoleId  = role.Id,
                Role    = role
            };

            // make sure the groupRole is not already present
            if (!group.Roles.Contains(newgroupRole))
            {
                group.Roles.Add(newgroupRole);
                _db.SaveChanges();
            }

            // Add all of the users in this group to the new role:
            IQueryable <ApplicationUser> groupUsers = _db.Users.Where(u => u.Groups.Any(g => g.GroupId == group.Id));

            foreach (ApplicationUser user in groupUsers)
            {
                if (!(_userManager.IsInRole(user.Id, roleName)))
                {
                    AddUserToRole(user.Id, role.Name);
                }
            }
        }
        public void CreateApplicationRoleGroup(int groupId, string roleName)
        {
            Group           group = _context.Groups.Find(groupId);
            ApplicationRole role  = _context.Roles.First(r => r.Name == roleName);

            var newgroupRole = new ApplicationRoleGroup
            {
                GroupId = group.Id,
                Group   = group,
                RoleId  = role.Id,
                Role    = role
            };

            // checking ! make sure the [ApplicationRoleGroups] is not exist
            if (!group.ApplicationRoleGroups.Contains(newgroupRole))
            {
                group.ApplicationRoleGroups.Add(newgroupRole);
                _context.SaveChanges();
            }

            // Add all of the users in this group to the new role:
            //IQueryable<ApplicationUser> groupUsers = _context.Users.Where(u => u.Groups.Any(g => g.GroupId == group.Id));

            foreach (ApplicationUser user in _context.Users.Where(u => u.Groups.Any(g => g.GroupId == groupId)).ToList())
            {
                if (!(_userManager.IsInRole(user.Id, roleName)))
                {
                    _userManager.AddToRole(user.Id, roleName);//AddUserToRole(user.Id, role.Name);
                }
            }
        }
        public void AddRoleToGroup(int groupId, string roleName)
        {
            var group = _db.Groups.Find(groupId);
            var role = _db.Roles.First(r => r.Name == roleName);
            var newgroupRole = new ApplicationRoleGroup()
            {
                GroupId = group.Id,
                Group = group,
                RoleId = role.Id,
                Role = (ApplicationRole)role
            };

            group.Roles.Add(newgroupRole);
            _db.SaveChanges();

            // Add all of the users in this group to the new role:
            var groupUsers = _db.Users.Where(u => u.Groups.Any(g => g.GroupId == group.Id));
            foreach (var user in groupUsers)
            {
                if (!(_userManager.IsInRole(user.Id, roleName)))
                {
                    this.AddUserToRole(user.Id, role.Name);
                }
            }
        }
        public void AddRoleToGroup(int groupId, string roleName)
        {
            Group group = _db.Groups.Find(groupId);
            ApplicationRole role = _db.Roles.First(r => r.Name == roleName);

            var newgroupRole = new ApplicationRoleGroup
            {
                GroupId = group.Id,
                Group = group,
                RoleId = role.Id,
                Role = role
            };

            // make sure the groupRole is not already present
            if (!group.Roles.Contains(newgroupRole))
            {
                group.Roles.Add(newgroupRole);
                _db.SaveChanges();
            }

            // Add all of the users in this group to the new role:
            IQueryable<ApplicationUser> groupUsers = _db.Users.Where(u => u.Groups.Any(g => g.GroupId == group.Id));
            foreach (ApplicationUser user in groupUsers)
            {
                if (!(_userManager.IsInRole(user.Id, roleName)))
                {
                    AddUserToRole(user.Id, role.Name);
                }
            }
        }