// GET: ApplicationUsers/Edit/5
        public async Task <ActionResult> Edit(string id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            ApplicationUser applicationUser = await ApplicationUsers.FindAsync(id);

            if (applicationUser == null)
            {
                return(HttpNotFound());
            }
            ViewBag.CustomerId = new SelectList(db.Customers, "CustomerId", "Name", applicationUser.CustomerId);

            var viewModel = new EditUserViewModel {
                Id = id, Email = applicationUser.Email, IsActive = applicationUser.IsActive
            };

            var userRoles = await _applicationUserManager.GetRolesAsync(id);

            viewModel.RolesList = _applicationRoleManager.Roles.ToList().Select(x => new SelectListItem()
            {
                Selected = userRoles.Contains(x.Name),
                Text     = x.Name,
                Value    = x.Name
            });

            return(View(viewModel));
        }
        public async Task <Dictionary <string, bool> > GetUserAccessAsync(string userId, IEnumerable <string> userRoles = null)
        {
            if (userRoles == null)
            {
                var user = await _userManagerService.FindByIdAsync(userId);

                userRoles = await _userManagerService.GetRolesAsync(user);
            }

            var userAccesses = _accessDictionary.Where(userAccess => userRoles.Contains(userAccess.Key));
            var accesses     = new Dictionary <string, bool>();

            foreach (var uAccess in userAccesses)
            {
                foreach (var roleAccesses in uAccess.Value)
                {
                    if (!accesses.ContainsKey(roleAccesses.Key))
                    {
                        accesses.Add(roleAccesses.Key, roleAccesses.Value);
                    }
                    else
                    {
                        accesses[roleAccesses.Key] |= roleAccesses.Value;
                    }
                }
            }

            return(accesses);
        }
Example #3
0
        public async Task <IActionResult> Edit(string userId)
        {
            if (!string.IsNullOrEmpty(userId))
            {
                var user = await _userManagerService.FindByIdAsync(userId);

                if (user == null)
                {
                    _logger.Log(LogLevel.Error, $"Can`t find the User");
                    return(RedirectToAction("HandleError", "Error", new { code = 404 }));
                }
                var userRoles = await _userManagerService.GetRolesAsync(user);

                var allRoles = await _adminService.GetRolesExceptAdminAsync();

                RoleViewModel model = new RoleViewModel
                {
                    UserID    = user.Id,
                    UserEmail = user.Email,
                    UserRoles = userRoles,
                    AllRoles  = allRoles
                };
                return(PartialView(model));
            }
            _logger.Log(LogLevel.Error, $"User, with userId: {userId}, is null");
            return(RedirectToAction("HandleError", "Error", new { code = 404 }));
        }
Example #4
0
        ///<inheritdoc/>
        public async Task <string> GenerateJWTTokenAsync(UserDTO userDTO)
        {
            var claims = new List <Claim>
            {
                new Claim(ClaimTypes.Name, userDTO.Email),
                new Claim(JwtRegisteredClaimNames.NameId, userDTO.Id),
                new Claim(JwtRegisteredClaimNames.FamilyName, userDTO.Id),
                new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString())
            };
            var roles = await _userManagerService.GetRolesAsync(userDTO);

            claims.AddRange(roles.Select(role => new Claim(ClaimsIdentity.DefaultRoleClaimType, role)));
            var key   = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_jwtOptions.Key));
            var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);

            var token = new JwtSecurityToken(
                issuer: _jwtOptions.Issuer,
                audience: _jwtOptions.Audience,
                claims: claims,
                expires: DateTime.Now.AddMinutes(_jwtOptions.Time),
                signingCredentials: creds);

            var tokenHandler = new JwtSecurityTokenHandler();

            return(tokenHandler.WriteToken(token));
        }
Example #5
0
        public async Task <IActionResult> Edit(string userId)
        {
            if (!string.IsNullOrEmpty(userId))
            {
                var user = await _userManagerService.FindByIdAsync(userId);

                if (user == null)
                {
                    _loggerService.LogError("User id is null");
                    return(NotFound());
                }
                var userRoles = await _userManagerService.GetRolesAsync(user);

                var allRoles = await _adminService.GetRolesExceptAdminAsync();

                RoleViewModel model = new RoleViewModel
                {
                    UserID    = user.Id,
                    UserEmail = user.Email,
                    UserRoles = userRoles,
                    AllRoles  = allRoles
                };

                return(Ok(model));
            }
            _loggerService.LogError("User id is null");
            return(NotFound());
        }
Example #6
0
        /// <inheritdoc />
        public async Task <IEnumerable <string> > GetUserAccessLevelsAsync(string userId)
        {
            List <string> accessLevels = new List <string>();
            var           user         = await _userManagerService.FindByIdAsync(userId);

            List <string> userRoles = (await _userManagerService.GetRolesAsync(user)).ToList();

            if (userRoles.Count == 1)
            {
                if (userRoles[0] == RolesForActiveMembershipTypeDTO.Plastun.GetDescription())
                {
                    accessLevels.Add(AccessLevelTypeDTO.Member.GetDescription());
                }
                else if (userRoles[0] == RolesForActiveMembershipTypeDTO.Supporter.GetDescription())
                {
                    accessLevels.Add(AccessLevelTypeDTO.Supporter.GetDescription());
                }
            }
            else if (userRoles.Count > 1)
            {
                accessLevels.AddRange(new List <string>
                {
                    AccessLevelTypeDTO.Member.GetDescription(),
                    AccessLevelTypeDTO.LeadershipMember.GetDescription()
                });
            }
            else
            {
                accessLevels.Add(AccessLevelTypeDTO.FormerMember.GetDescription());
            }

            return(accessLevels.AsEnumerable());
        }
Example #7
0
        /// <inheritdoc />
        public async Task <IEnumerable <string> > GetUserAccessLevelsAsync(string userId)
        {
            var leadershipLevelRoles = new List <string>
            {
                Roles.KurinHead,
                Roles.KurinHeadDeputy,
                Roles.KurinSecretary,
                Roles.CityHead,
                Roles.CityHeadDeputy,
                Roles.CitySecretary,
                Roles.OkrugaHead,
                Roles.OkrugaHeadDeputy,
                Roles.OkrugaSecretary,
                "Голова Керівного органу",
                "Діловод Керівного органу"
            };

            var supporterLevelDegree = "Пласт прият";
            var accessLevels         = new List <string>();
            var user = await _userManagerService.FindByIdAsync(userId);

            var userRoles        = (await _userManagerService.GetRolesAsync(user)).ToList();
            var userPlastDegrees = await _plastDegreeService.GetUserPlastDegreesAsync(userId);

            var isInSupporterDegree = userPlastDegrees.Any(d => d.PlastDegree.Name == supporterLevelDegree);

            if (userRoles.Contains(RolesForActiveMembershipTypeDTO.RegisteredUser.GetDescription()))
            {
                accessLevels.Add(AccessLevelTypeDTO.RegisteredUser.GetDescription());
            }

            if (userRoles.Contains(RolesForActiveMembershipTypeDTO.Supporter.GetDescription()) ||
                userRoles.Contains(RolesForActiveMembershipTypeDTO.FormerPlastMember.GetDescription()) ||
                isInSupporterDegree)
            {
                accessLevels.Add(AccessLevelTypeDTO.Supporter.GetDescription());
            }

            if (userRoles.Contains(RolesForActiveMembershipTypeDTO.PlastMember.GetDescription()))
            {
                accessLevels.Add(AccessLevelTypeDTO.PlastMember.GetDescription());
            }

            if (userRoles.Intersect(leadershipLevelRoles).Any())
            {
                accessLevels.Add(AccessLevelTypeDTO.LeadershipMember.GetDescription());
            }

            return(accessLevels.AsEnumerable());
        }
        public async Task <IActionResult> Approvers(string userId, string approverId)
        {
            if (string.IsNullOrEmpty(userId))
            {
                _loggerService.LogError("User id is null");
                return(NotFound());
            }

            UserDTO user;

            try
            {
                user = await _userService.GetUserAsync(userId);
            }
            catch (Exception)
            {
                _loggerService.LogError($"User not found. UserId:{userId}");
                return(NotFound());
            }
            var currentUserId = _userManager.GetUserId(User);
            var currentUser   = await _userService.GetUserAsync(currentUserId);

            var userRoles             = (await _userManagerService.GetRolesAsync(user)).ToList();
            var confirmedUsers        = _userService.GetConfirmedUsers(user);
            var canApprove            = !userRoles.Any(r => r == Roles.RegisteredUser || r == Roles.FormerPlastMember);
            var canApprovePlastMember = canApprove && _userService.CanApprove(confirmedUsers, userId, currentUserId,
                                                                              await _userManagerService.IsInRoleAsync(currentUser, Roles.Admin));
            var time         = _userService.CheckOrAddPlastunRole(user.Id, user.RegistredOn);
            var clubApprover = _userService.GetClubAdminConfirmedUser(user);
            var cityApprover = _userService.GetCityAdminConfirmedUser(user);

            var model = new UserApproversViewModel
            {
                User                     = _mapper.Map <UserDTO, UserInfoViewModel>(user),
                CanApprove               = canApprove,
                CanApprovePlastMember    = canApprovePlastMember,
                TimeToJoinPlast          = ((int)time.TotalDays),
                ConfirmedUsers           = _mapper.Map <IEnumerable <ConfirmedUserDTO>, IEnumerable <ConfirmedUserViewModel> >(confirmedUsers),
                ClubApprover             = _mapper.Map <ConfirmedUserDTO, ConfirmedUserViewModel>(clubApprover),
                CityApprover             = _mapper.Map <ConfirmedUserDTO, ConfirmedUserViewModel>(cityApprover),
                IsUserHeadOfCity         = await _userManagerService.IsInRoleAsync(_mapper.Map <User, UserDTO>(await _userManager.GetUserAsync(User)), Roles.CityHead),
                IsUserHeadDeputyOfCity   = await _userManagerService.IsInRoleAsync(_mapper.Map <User, UserDTO>(await _userManager.GetUserAsync(User)), Roles.CityHeadDeputy),
                IsUserHeadOfClub         = await _userManagerService.IsInRoleAsync(_mapper.Map <User, UserDTO>(await _userManager.GetUserAsync(User)), Roles.KurinHead),
                IsUserHeadDeputyOfClub   = await _userManagerService.IsInRoleAsync(_mapper.Map <User, UserDTO>(await _userManager.GetUserAsync(User)), Roles.KurinHeadDeputy),
                IsUserHeadOfRegion       = await _userManagerService.IsInRoleAsync(_mapper.Map <User, UserDTO>(await _userManager.GetUserAsync(User)), Roles.OkrugaHead),
                IsUserHeadDeputyOfRegion = await _userManagerService.IsInRoleAsync(_mapper.Map <User, UserDTO>(await _userManager.GetUserAsync(User)), Roles.OkrugaHeadDeputy),
                IsUserPlastun            = await _userManagerService.IsInRoleAsync(user, Roles.PlastMember) ||
                                           user.UserProfile.UpuDegreeID != 1 ||
                                           !(await _userManagerService.IsInRoleAsync(user, Roles.Supporter) &&
                                             await _userService.IsApprovedCityMember(userId)),
                CurrentUserId = approverId
            };

            foreach (var item in model.ConfirmedUsers)
            {
                item.Approver.User.ImagePath = await _userService.GetImageBase64Async(item.Approver.User.ImagePath);
            }

            if (model.ClubApprover != null)
            {
                model.ClubApprover.Approver.User.ImagePath = await _userService.GetImageBase64Async(model?.ClubApprover?.Approver?.User.ImagePath);
            }
            if (model.CityApprover != null)
            {
                model.CityApprover.Approver.User.ImagePath = await _userService.GetImageBase64Async(model?.CityApprover?.Approver?.User.ImagePath);
            }
            return(Ok(model));
        }