示例#1
0
        public async Task <bool> SetUserActive(ClaimsPrincipal currentUser, string userId, bool status)
        {
            var user = _userManager.Users.SingleOrDefault(u => u.Id == userId);

            if (user == null)
            {
                return(false);
            }
            if (user.UserName == currentUser.Identity.Name)
            {
                return(false);
            }

            if (!currentUser.IsInRole(ApplicationRoleManager.MultiSyndicate))
            {
                var currentUserSyndicate = await _userManager.GetSyndicateOverrideAsync(currentUser);

                if (!currentUserSyndicate.HasValue)
                {
                    var currentUserPlayerId = await _userManager.GetPlayerIdAsync(currentUser);

                    if (currentUserPlayerId.HasValue)
                    {
                        var currentUserPlayer = _dbContext.Player.SingleOrDefault(p => p.Id == currentUserPlayerId);
                        if (currentUserPlayer != null)
                        {
                            currentUserSyndicate = currentUserPlayer.SyndicateId;
                        }
                    }
                }
                var userSyndicate = user.SyndicateOverride;
                if (!userSyndicate.HasValue)
                {
                    var userPlayer = _dbContext.Player.SingleOrDefault(p => p.Id == user.PlayerId);
                    if (userPlayer != null)
                    {
                        userSyndicate = userPlayer.SyndicateId;
                    }
                }
                if (currentUserSyndicate != userSyndicate)
                {
                    return(false);
                }
            }

            var roles = await _userManager.GetRolesAsync(user);

            var maxRole = roles.Contains(ApplicationRoleManager.Administrator) ? ApplicationRoleManager.Administrator :
                          roles.Contains(ApplicationRoleManager.Leader) ? ApplicationRoleManager.Leader :
                          roles.Contains(ApplicationRoleManager.Officer) ? ApplicationRoleManager.Officer : "";

            if (!ApplicationRoleManager.CanActivate(currentUser, maxRole))
            {
                return(false);
            }
            user.IsApproved = status;
            await _userManager.UpdateAsync(user);

            return(true);
        }
示例#2
0
        public async Task <UserListViewModel> GetUserList(ClaimsPrincipal currentUser, Syndicate syndicate)
        {
            var loadAll = currentUser.IsInRole(ApplicationRoleManager.MultiSyndicate);

            if (!loadAll && syndicate == null)
            {
                return new UserListViewModel {
                           Players = new List <Player>(),
                           Roles   = new Dictionary <string, bool>(),
                           Users   = new List <UserViewModel>(),
                }
            }
            ;

            var playerList = _dbContext.Player.Where(p => loadAll || p.SyndicateId == syndicate.Id).OrderBy(p => p.Name).AsEnumerable();
            var roleList   = _roleManager.Roles.ToDictionary(r => r.Name, r => ApplicationRoleManager.CanEdit(currentUser, r.Name));
            var userList   = _userManager.Users
                             .AsEnumerable() //TODO: !BAD! for performance - think about the way to rewrite it
                             .Where(
                u => loadAll || playerList.Any(p => p.Id == u.PlayerId) || u.SyndicateOverride == syndicate.Id
                )
                             .OrderBy(u => u.UserName)
                             .Select(u => new UserViewModel
            {
                User            = u,
                Player          = null,                                          // have to select it further down the road
                Roles           = roleList.ToDictionary(r => r.Key, r => false), // We need to populate the roles later since ET lambdas do not support async ops
                AllowActivation = false,                                         // Same as above
            }
                                     ).ToArray();

            for (var i = 0; i < userList.Count(); i++)
            {
                userList[i].Player = playerList.SingleOrDefault(p => p.Id == userList[i].User.PlayerId);
                var userRoles = await _userManager.GetRolesAsync(userList[i].User);

                foreach (var role in roleList.Keys)
                {
                    userList[i].Roles[role] = userRoles.Contains(role);
                }
                var maxRole = userRoles.Contains(ApplicationRoleManager.Administrator) ? ApplicationRoleManager.Administrator :
                              userRoles.Contains(ApplicationRoleManager.Leader) ? ApplicationRoleManager.Leader :
                              userRoles.Contains(ApplicationRoleManager.Officer) ? ApplicationRoleManager.Officer : "";
                userList[i].AllowActivation = userList[i].User.UserName == currentUser.Identity.Name ? false : ApplicationRoleManager.CanActivate(currentUser, maxRole);
            }
            return(new UserListViewModel {
                Roles = roleList,
                Users = userList,
                Players = playerList,
            });
        }