コード例 #1
0
        /// <summary>
        /// This method requires that you already validated all parameters
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="groupId"></param>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public async Task <Result <GroupUserEntity> > AddUserToGroupWithoutValidation(string userId, string groupId, string roleId)
        {
            Result beforeResult = await _addGroupUserFilter.BeforeAdd(userId, groupId, roleId);

            if (beforeResult.Failure)
            {
                return(Result.Fail <GroupUserEntity>(beforeResult));
            }

            GroupUserEntity groupUser = new GroupUserEntity(
                userId: userId,
                groupId: groupId,
                roleId: roleId);

            bool addResult = await _groupUserDAO.Add(groupUser);

            if (!addResult)
            {
                _logger.LogError($"Failed to add GroupUser. GroupId {groupId}, UserId {userId}, RoleId {roleId}");
                return(Result.Fail <GroupUserEntity>(FAILED_TO_ADD_GROUP_USER));
            }

            Result afterResult = await _addGroupUserFilter.AfterAdded(groupUser);

            if (afterResult.Failure)
            {
                return(Result.Fail <GroupUserEntity>(afterResult));
            }

            return(Result.Ok(groupUser));
        }
コード例 #2
0
        public T AddItem(T newObject)
        {
            var re = newObject == null ? default(T) : _db.Add(newObject);

            if (_cache != null && _CACHEKEY != null && re != null)
            {
                _cache.MarkChanged(_CACHEKEY);
            }
            return(re);
        }
コード例 #3
0
ファイル: GroupService.cs プロジェクト: weedkiller/IdentityUI
        public async Task <Result <IdStringModel> > AddAsync(AddGroupRequest addGroup)
        {
            ValidationResult validationResult = _addGroupValidator.Validate(addGroup);

            if (!validationResult.IsValid)
            {
                _logger.LogWarning($"Invalid AddGroupRequest model");
                return(Result.Fail <IdStringModel>(validationResult.ToResultError()));
            }

            addGroup.Name = addGroup.Name.Trim();

            IBaseSpecification <GroupEntity, GroupEntity> groupExistSpecification = SpecificationBuilder
                                                                                    .Create <GroupEntity>()
                                                                                    .WithName(addGroup.Name)
                                                                                    .Build();

            bool groupExist = await _groupDAO.Exist(groupExistSpecification);

            if (groupExist)
            {
                _logger.LogError($"Group with the same name already exist. GroupName {addGroup.Name}");
                return(Result.Fail <IdStringModel>(GROUP_WITH_NAME_ALREADY_EXIST));
            }

            GroupEntity group = new GroupEntity(
                name: addGroup.Name);

            bool addResult = await _groupDAO.Add(group);

            if (!addResult)
            {
                _logger.LogError($"Failed to add group");
                return(Result.Fail <IdStringModel>(FAILED_TO_ADD_GROUP));
            }

            IdStringModel idStringModel = new IdStringModel(
                id: group.Id);

            return(Result.Ok(idStringModel));
        }
コード例 #4
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());
        }
コード例 #5
0
        public async Task <Core.Models.Result.Result> UpdateProfileImage(string userId, UploadProfileImageRequest uploadProfileImageRequest)
        {
            if (uploadProfileImageRequest == null || uploadProfileImageRequest.File == null)
            {
                _logger.LogWarning($"No image. UserId {userId}");
                return(Result.Fail(PROFILE_IMAGE_NOT_FOUND).ToOldResult());
            }

            //TODO: changed how image format is validated
            string imageExtension = Path.GetExtension(uploadProfileImageRequest.File.FileName);

            if (!VALID_IMAGE_FORMATS.Contains(imageExtension.ToUpper()))
            {
                _logger.LogWarning($"Invalid image format. UserId {userId}, image extension {imageExtension}");
                return(Result.Fail(INVALID_PROFILE_IMAGE_FORMAT, VALID_IMAGE_FORMATS).ToOldResult());
            }

            if (uploadProfileImageRequest.File.Length > _identityUIOptions.MaxProfileImageSize)
            {
                _logger.LogWarning($"Image is to big. UserId {userId}, image size {uploadProfileImageRequest.File.Length}");
                return(Result.Fail(PROFILE_IMAGE_TOO_BIG, _identityUIOptions.MaxProfileImageSize / 1024).ToOldResult());
            }

            if (uploadProfileImageRequest.File.FileName.Length > 250)
            {
                _logger.LogWarning($"Image name is to long. Image name length {uploadProfileImageRequest.File.FileName.Length}");
                return(Result.Fail(PROFILE_IMAGE_NAME_TOO_LONG, 250).ToOldResult());
            }

            byte[] image;

            using (MemoryStream memoryStream = new MemoryStream())
            {
                await uploadProfileImageRequest.File.CopyToAsync(memoryStream);

                image = memoryStream.ToArray();
            }

            string blobCacheKey = string.Format(BLOBL_CACHE_KEY, userId);
            string urlCacheKey  = string.Format(URL_CACHE_KEY, userId);

            _memoryCache.Remove(blobCacheKey);
            _memoryCache.Remove(urlCacheKey);

            IBaseSpecification <UserImageEntity, UserImageEntity> getUserImageSpecification = SpecificationBuilder
                                                                                              .Create <UserImageEntity>()
                                                                                              .Where(x => x.UserId == userId)
                                                                                              .Build();

            UserImageEntity userImage = await _userImageDAO.SingleOrDefault(getUserImageSpecification);

            if (userImage == null)
            {
                UserImageEntity newUserImage = new UserImageEntity(
                    userId: userId,
                    blobImage: image,
                    fileName: uploadProfileImageRequest.File.FileName);

                bool addResult = await _userImageDAO.Add(newUserImage);

                if (!addResult)
                {
                    _logger.LogError($"Failed to add user image. UserId {userId}");
                    return(Result.Fail(FAILED_TO_ADD_PROFILE_IMAGE).ToOldResult());
                }

                return(Result.Ok().ToOldResult());
            }

            userImage.BlobImage = image;
            userImage.FileName  = uploadProfileImageRequest.File.FileName;

            bool updateResult = await _userImageDAO.Update(userImage);

            if (!updateResult)
            {
                _logger.LogError($"Failed to update user image. UserId {userId}");
                return(Result.Fail(FAILED_TO_UPDATE_PROFILE_IMAGE).ToOldResult());
            }

            return(Result.Ok().ToOldResult());
        }