예제 #1
0
        public Task <Result> Add(string code, string userId)
        {
            string remoteIp = _httpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString();

            Core.Models.Result.Result result = Add(code, userId, remoteIp);

            return(Task.FromResult(result.ToNewResult()));
        }
예제 #2
0
        private async Task <Result> ChangeRoleAsync(long groupUserId, string roleId, string userId)
        {
            _logger.LogInformation($"Changing GroupUser role. GroupUserId {groupUserId}, roleId {roleId}");

            Result roleValidResult = await RoleIsValid(roleId);

            if (roleValidResult.Failure)
            {
                return(Result.Fail(roleValidResult));
            }

            List <RoleListData> canAssigneGroupRoles = _groupUserStore.CanAssigneGroupRoles();

            if (!canAssigneGroupRoles.Any(x => x.Id == roleId))
            {
                _logger.LogError($"User does not have permission to assign role. RoleId {roleId}");
                return(Result.Fail(NO_PERMISSION));
            }

            Core.Models.Result.Result <GroupUserEntity> getGroupUserResult = _groupUserStore.Get(groupUserId);
            if (getGroupUserResult.Failure)
            {
                return(getGroupUserResult.ToNewResult());
            }

            GroupUserEntity groupUser = getGroupUserResult.Value;

            List <RoleListData> canManageGroupRoles = _groupUserStore.CanManageGroupRoles();

            if (!canManageGroupRoles.Any(x => x.Id != groupUser.RoleId))
            {
                _logger.LogError($"User does not have permission to manage role. GroupUserId {groupUserId} RoleId {roleId}");
                return(Result.Fail(NO_PERMISSION));
            }

            if (!_groupUserStore.CanChangeOwnRole())
            {
                if (groupUser.UserId == userId)
                {
                    _logger.LogError($"User can not change his own role");
                    return(Result.Fail(USER_CAN_NOT_CHANGE_HIS_OWN_ROLE));
                }
            }

            groupUser.UpdateRole(roleId);

            bool updateResult = await _groupUserDAO.Update(groupUser);

            if (!updateResult)
            {
                _logger.LogError($"Failed to change group user role. GroupUserId {groupUserId}, roleId {roleId}");
                return(Result.Fail(FAILED_TO_CAHNGE_GROUP_USER_ROLE));
            }

            return(Result.Ok());
        }
        public async Task <IActionResult> Invite([FromRoute] string groupId, [FromBody] InviteToGroupRequest inviteToGroupRequest)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            Core.Models.Result.Result result = await _inviteService.InviteToGroup(groupId, inviteToGroupRequest);

            return(result.ToNewResult().ToApiResult());
        }
        public IActionResult Get([FromRoute] string groupId, [FromQuery] DataTableRequest dataTableRequest)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            Core.Models.Result.Result <DataTableResult <GroupInviteTableModel> > result = _groupInviteDataService.Get(groupId, dataTableRequest);

            return(result.ToNewResult().ToApiResult());
        }
예제 #5
0
        public async Task <CommonUtils.Result.Result> EditUser(long groupUserId, EditUserRequest editUserRequest)
        {
            IBaseSpecification <GroupUserEntity, GroupUserEntity> specification = SpecificationBuilder
                                                                                  .Create <GroupUserEntity>()
                                                                                  .Where(x => x.Id == groupUserId)
                                                                                  .Include(x => x.User)
                                                                                  .Build();

            CommonUtils.Result.Result <GroupUserEntity> getGroupUserResult = await _groupUserStore.SingleOrDefault(specification);

            if (getGroupUserResult.Failure)
            {
                return(CommonUtils.Result.Result.Fail(getGroupUserResult));
            }

            //TODO: change so that Edit takes AppUserEntity as parameter
            Core.Models.Result.Result updateResult = await EditUser(getGroupUserResult.Value.UserId, editUserRequest, "");

            return(updateResult.ToNewResult());
        }
예제 #6
0
        public async Task <CommonUtils.Result.Result> SendEmilVerificationMail(long groupUserId)
        {
            IBaseSpecification <GroupUserEntity, GroupUserEntity> specification = SpecificationBuilder
                                                                                  .Create <GroupUserEntity>()
                                                                                  .Where(x => x.Id == groupUserId)
                                                                                  .Include(x => x.User)
                                                                                  .Build();

            CommonUtils.Result.Result <GroupUserEntity> getGroupUserResult = await _groupUserStore.SingleOrDefault(specification);

            if (getGroupUserResult.Failure)
            {
                return(CommonUtils.Result.Result.Fail(getGroupUserResult));
            }

            SendEmailVerificationMailRequest sendEmailVerificationMailRequest = new SendEmailVerificationMailRequest(
                userId: getGroupUserResult.Value.UserId);

            Core.Models.Result.Result result = await SendEmilVerificationMail(sendEmailVerificationMailRequest, "");

            return(result.ToNewResult());
        }
예제 #7
0
        public Task <IActionResult> Delete([FromRoute] string groupId, [FromRoute] long groupAttributeId)
        {
            Core.Models.Result.Result result = _groupAttributeService.Remove(groupId, groupAttributeId);

            return(Task.FromResult(result.ToNewResult().ToApiResult()));
        }
예제 #8
0
        public Task <IActionResult> Update([FromRoute] string groupId, [FromRoute] long groupAttributeId, [FromBody] EditGroupAttributeRequest request)
        {
            Core.Models.Result.Result result = _groupAttributeService.Edit(groupId, groupAttributeId, request);

            return(Task.FromResult(result.ToNewResult().ToApiResult()));
        }
예제 #9
0
        public Task <IActionResult> Add([FromRoute] string groupId, [FromBody] AddGroupAttributeRequest request)
        {
            Core.Models.Result.Result result = _groupAttributeService.Add(groupId, request);

            return(Task.FromResult(result.ToNewResult().ToApiResult()));
        }
예제 #10
0
        public async Task <IActionResult> Add([FromRoute] string groupId, [FromBody] InviteToGroupRequest request)
        {
            Core.Models.Result.Result result = await _inviteService.InviteToGroup(groupId, request);

            return(result.ToNewResult().ToApiResult());
        }
예제 #11
0
        public async Task <Result> AddInvite(string email, string roleId = null, string groupId = null, string groupRoleId = null)
        {
            Result inviteAlreadyExistsResult = await InviteAlreadyExits(email);

            if (inviteAlreadyExistsResult.Failure)
            {
                return(inviteAlreadyExistsResult);
            }

            //TODO: change method or response to make more sense
            Result userAlreadyExistsResult = await UserAlreadyExist(email);

            if (userAlreadyExistsResult.Failure)
            {
                return(userAlreadyExistsResult);
            }

            if (!string.IsNullOrEmpty(roleId))
            {
                Result roleValidResult = await GlobalRoleExists(roleId);

                if (roleValidResult.Failure)
                {
                    return(roleValidResult);
                }
            }

            if (!string.IsNullOrEmpty(groupId))
            {
                Result isGroupValid = await IsGroupInviteValid(groupId, groupRoleId);

                if (isGroupValid.Failure)
                {
                    return(isGroupValid);
                }
            }

            Result beforeAddResult = await _addInviteFilter.BeforeAdd(email, roleId, groupId, groupRoleId);

            if (beforeAddResult.Failure)
            {
                return(beforeAddResult);
            }

            InviteEntity invite = new InviteEntity(
                email: email,
                token: StringUtils.GenerateToken(),
                status: Data.Enums.Entity.InviteStatuses.Pending,
                roleId: roleId,
                groupId: groupId,
                groupRoleId: groupRoleId,
                expiresAt: DateTimeOffset.UtcNow.Add(_identityManagementEndpoints.InviteValidForTimeSpan));

            bool addInvite = await _inviteDAO.Add(invite);

            if (!addInvite)
            {
                _logger.LogError($"Failed to add invite");
                return(Result.Fail(FAILED_TO_ADD_INVITE));
            }

            Result afterAddedResult = await _addInviteFilter.AfterAdded(invite);

            if (afterAddedResult.Failure)
            {
                return(afterAddedResult);
            }

            _logger.LogInformation($"Invite was added, sending email");

            string callbackUrl = QueryHelpers.AddQueryString($"{_identityManagementOptions.BasePath}{_identityManagementEndpoints.AcceptInvite}", "code", invite.Token);

            callbackUrl = HtmlEncoder.Default.Encode(callbackUrl);

            Core.Models.Result.Result sendMailResult = await _mailService.SendInvite(invite.Email, callbackUrl);

            if (sendMailResult.Failure)
            {
                return(sendMailResult.ToNewResult());
            }

            return(Result.Ok());
        }