Пример #1
0
        private async Task <TResponse <int> > CheckInvalidPassword(string username,
                                                                   string password)
        {
            try
            {
                var result = await ReadOnlyRepository.QueryFirstOrDefaultAsync <int>(SqlQuery.USER_CHECK_PASSWORD, new
                {
                    Username = username,
                    Password = Sha512(password)
                });

                if (result.IsSuccess)
                {
                    if (result.Data > 0)
                    {
                        return(await Ok(result.Data));
                    }

                    return(await Fail <int>(ErrorEnum.PasswordNotMatch.GetStringValue()));
                }

                return(await Fail <int>(result.Message));
            }
            catch (Exception exception)
            {
                return(await Fail <int>(exception));
            }
        }
Пример #2
0
        private async Task <TResponse <bool> > CanInsert(InsertProductModel request)
        {
            try
            {
                var product = await ReadOnlyRepository.QueryFirstOrDefaultAsync <ProductModel>(SqlQuery.PRODUCT_FIND_BY_NAME,
                                                                                               new
                {
                    request.Name
                });

                if (product.IsSuccess)
                {
                    if (product.Data != null)
                    {
                        return(await Fail <bool>(ErrorEnum.PRODUCT_HAS_EXIST.GetStringValue()));
                    }

                    return(await Ok(true));
                }

                return(await Fail <bool>(product.Message));
            }
            catch (Exception exception)
            {
                return(await Fail <bool>(exception));
            }
        }
Пример #3
0
        public async Task <TResponse <IEnumerable <PersistedGrantDto> > > GetAllBySubjectId(string subjectId)
        {
            try
            {
                var result = await ReadOnlyRepository.QueryFirstOrDefaultAsync <IEnumerable <PersistedGrantDto> >(SqlQuery.PERSISTED_GRANT_GET_BY_SUBJECT_ID, new
                {
                    SubjectId = subjectId
                });

                if (result.IsSuccess)
                {
                    if (result.Data != null)
                    {
                        return(await Ok(result.Data));
                    }

                    return(await Fail <IEnumerable <PersistedGrantDto> >(ErrorEnum.PersistedGrantHasNotExist.GetStringValue()));
                }

                return(await Fail <IEnumerable <PersistedGrantDto> >(result.Message));
            }
            catch (Exception exception)
            {
                return(await Fail <IEnumerable <PersistedGrantDto> >(exception));
            }
        }
Пример #4
0
        private async Task <TResponse <bool> > CheckUsernameHasExist(string username)
        {
            try
            {
                var result = await ReadOnlyRepository.QueryFirstOrDefaultAsync <int>(SqlQuery.USER_FIND_BY_USERNAME, new
                {
                    Username = username
                });

                if (result.IsSuccess)
                {
                    if (result.Data > 0)
                    {
                        return(await Ok(true));
                    }

                    return(await Fail <bool>(ErrorEnum.UsernameHasNotExist.GetStringValue()));
                }

                return(await Fail <bool>(result.Message));
            }
            catch (Exception exception)
            {
                return(await Fail <bool>(exception));
            }
        }
Пример #5
0
        public async Task <TResponse <PersistedGrantDto> > GetByKey(string key)
        {
            try
            {
                var result = await ReadOnlyRepository.QueryFirstOrDefaultAsync <PersistedGrantDto>(SqlQuery.PERSISTED_GRANT_GET_BY_KEY, new
                {
                    Key = key
                });

                if (result.IsSuccess)
                {
                    if (result.Data != null)
                    {
                        return(await Ok(result.Data));
                    }

                    return(await Fail <PersistedGrantDto>(ErrorEnum.PersistedGrantHasNotExist.GetStringValue()));
                }

                return(await Fail <PersistedGrantDto>(result.Message));
            }
            catch (Exception exception)
            {
                return(await Fail <PersistedGrantDto>(exception));
            }
        }
Пример #6
0
        public async Task <TResponse <RoleModel> > GetByUserId(int userId)
        {
            try
            {
                var result = await ReadOnlyRepository.QueryFirstOrDefaultAsync <RoleModel>(SqlQuery.ROLE_GET_BY_USER_ID, new
                {
                    UserId = userId
                });

                if (result.IsSuccess)
                {
                    if (result.Data != null)
                    {
                        return(await Ok(result.Data));
                    }

                    return(await Fail <RoleModel>(ErrorEnum.ROLE_HAS_NOT_EXIST.GetStringValue()));
                }

                return(await Fail <RoleModel>(result.Message));
            }
            catch (Exception exception)
            {
                return(await Fail <RoleModel>(exception));
            }
        }
Пример #7
0
        private async Task <TResponse <bool> > CanUpdate(UpdateRoleModel request)
        {
            try
            {
                var role = await ReadOnlyRepository.QueryFirstOrDefaultAsync <RoleModel>(SqlQuery.ROLE_FIND_BY_NAME_AND_ID,
                                                                                         new
                {
                    request.Name,
                    request.Id
                });

                if (role.IsSuccess)
                {
                    if (role.Data != null)
                    {
                        return(await Fail <bool>(ErrorEnum.ROLE_HAS_EXIST.GetStringValue()));
                    }

                    return(await Ok(true));
                }

                return(await Fail <bool>(role.Message));
            }
            catch (Exception exception)
            {
                return(await Fail <bool>(exception));
            }
        }
Пример #8
0
        private async Task <TResponse <bool> > CanInsert(InsertRoleModel request)
        {
            try
            {
                var checkValidName = await ReadOnlyRepository.QueryFirstOrDefaultAsync <RoleModel>(SqlQuery.ROLE_GET_BY_NAME, new
                {
                    request.Name
                });

                if (checkValidName.IsSuccess)
                {
                    if (checkValidName.Data != null)
                    {
                        return(await Fail <bool>(ErrorEnum.RoleNameHasExist.GetStringValue()));
                    }

                    else
                    {
                        return(await Ok(true));
                    }
                }

                return(await Fail <bool>(checkValidName.Message));
            }
            catch (Exception exception)
            {
                return(await Fail <bool>(exception));
            }
        }
Пример #9
0
        private async Task <TResponse <bool> > CheckInvalidSite(int userId,
                                                                int siteId)
        {
            try
            {
                var result = await ReadOnlyRepository.QueryFirstOrDefaultAsync <int>(SqlQuery.USER_CHECK_BY_SITE, new
                {
                    UserId = userId,
                    SiteId = siteId
                });

                if (result.IsSuccess)
                {
                    if (result.Data > 0)
                    {
                        return(await Ok(true));
                    }

                    return(await Fail <bool>(ErrorEnum.UserNotBelongThisSite.GetStringValue()));
                }

                return(await Fail <bool>(result.Message));
            }
            catch (Exception exception)
            {
                return(await Fail <bool>(exception));
            }
        }
Пример #10
0
        private async Task <TResponse <bool> > CanInsert(InsertUserModel request)
        {
            try
            {
                var user = await ReadOnlyRepository.QueryFirstOrDefaultAsync <UserModel>(SqlQuery.USER_FIND_BY_USERNAME,
                                                                                         new
                {
                    request.Username
                });

                if (user.IsSuccess)
                {
                    if (user.Data != null)
                    {
                        return(await Fail <bool>(ErrorEnum.USERNAME_HAS_EXIST.GetStringValue()));
                    }

                    return(await Ok(true));
                }

                return(await Fail <bool>(user.Message));
            }
            catch (Exception exception)
            {
                return(await Fail <bool>(exception));
            }
        }
Пример #11
0
        /// <summary>
        ///     Get Site By Id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task <TResponse <SiteModel> > GetById(int id)
        {
            try
            {
                var result = await ReadOnlyRepository.QueryFirstOrDefaultAsync <SiteModel>(
                    SqlQuery.SITE_GET_BY_ID, new
                {
                    Id = id
                });

                if (result.IsSuccess)
                {
                    if (result.Data != null)
                    {
                        return(await Ok(result.Data));
                    }

                    return(await Fail <SiteModel>(ErrorEnum.SiteIdHasNotExist.GetStringValue()));
                }

                return(await Fail <SiteModel>(result.Message));
            }
            catch (Exception exception)
            {
                return(await Fail <SiteModel>(exception));
            }
        }
Пример #12
0
        private async Task <TResponse <bool> > CanDelete(int id)
        {
            try
            {
                var role = await ReadOnlyRepository.QueryFirstOrDefaultAsync <RoleModel>(SqlQuery.ROLE_GET_BY_ID,
                                                                                         new
                {
                    Id = id
                });

                if (role.IsSuccess)
                {
                    if (role.Data != null)
                    {
                        return(await Ok(true));
                    }

                    return(await Fail <bool>(ErrorEnum.ROLE_HAS_NOT_EXIST.GetStringValue()));
                }

                return(await Fail <bool>(role.Message));
            }
            catch (Exception exception)
            {
                return(await Fail <bool>(exception));
            }
        }
Пример #13
0
        public async Task <TResponse <UserModel> > GetByUsername(string username)
        {
            try
            {
                var result = await ReadOnlyRepository.QueryFirstOrDefaultAsync <UserModel>(SqlQuery.USER_FIND_BY_USERNAME,
                                                                                           new
                {
                    Username = username
                });

                if (result.IsSuccess)
                {
                    if (result.Data != null)
                    {
                        return(await Ok(result.Data));
                    }

                    return(await Fail <UserModel>(ErrorEnum.USER_HAS_NOT_EXIST.GetStringValue()));
                }

                return(await Fail <UserModel>(result.Message));
            }
            catch (Exception exception)
            {
                return(await Fail <UserModel>(exception));
            }
        }
Пример #14
0
        public async Task <TResponse <UserAgent> > GetByName(string name)
        {
            try
            {
                string sqlQuery  = @"SELECT * FROM dbo.UserAgent WHERE Name=@Name";
                var    userAgent = await ReadOnlyRepository.QueryFirstOrDefaultAsync <UserAgent>(sqlQuery,
                                                                                                 new
                {
                    Name = name
                });

                if (userAgent.IsSuccess)
                {
                    if (userAgent.Data != null)
                    {
                        return(await Ok(userAgent.Data));
                    }

                    return(await Fail <UserAgent>(""));
                }

                return(await Fail <UserAgent>(userAgent.Message));
            }
            catch (Exception exception)
            {
                return(await Fail <UserAgent>(exception));
            }
        }
Пример #15
0
        /// <summary>
        ///     Get User By Id
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public async Task <TResponse <UserModel> > GetById(int userId)
        {
            try
            {
                var result = await ReadOnlyRepository.QueryFirstOrDefaultAsync <UserModel>(
                    SqlQuery.USER_GET_BY_ID, new
                {
                    Id = userId
                });

                if (result != null)
                {
                    if (result.IsSuccess)
                    {
                        if (result.Data != null)
                        {
                            var user = result.Data;

                            var sites = await ReadOnlyRepository.QueryAsync <SiteModel>(SqlQuery.SITE_GET_BY_USER_ID, new
                            {
                                UserId = user.Id
                            });

                            if (sites.IsSuccess &&
                                sites.Data != null &&
                                sites.Data.Any())
                            {
                                user.Sites.AddRange(sites.Data.Select(c => c.Id));
                            }

                            var roles = await ReadOnlyRepository.QueryAsync <RoleModel>(SqlQuery.ROLE_GET_BY_USER_ID, new
                            {
                                UserId = user.Id
                            });

                            if (roles.IsSuccess &&
                                roles.Data != null &&
                                roles.Data.Any())
                            {
                                user.Roles.AddRange(roles.Data.Select(c => c.Id));
                            }

                            return(await Ok(user));
                        }

                        return(await Fail <UserModel>(ErrorEnum.UserHasNotExist.GetStringValue()));
                    }

                    return(await Fail <UserModel>(result.Message));
                }

                return(await Ok <UserModel>(null));
            }
            catch (Exception exception)
            {
                return(await Fail <UserModel>(exception));
            }
        }
Пример #16
0
        private async Task <TResponse <bool> > CanInsert(InsertSiteModel request)
        {
            try
            {
                var checkValidId = await ReadOnlyRepository.QueryFirstOrDefaultAsync <SiteModel>(SqlQuery.SITE_GET_BY_ID,
                                                                                                 new
                {
                    request.Id
                });

                if (checkValidId.IsSuccess)
                {
                    if (checkValidId.Data != null)
                    {
                        return(await Fail <bool>(ErrorEnum.SiteIdHasExist.GetStringValue()));
                    }

                    var checkValidSiteCode = await ReadOnlyRepository.QueryFirstOrDefaultAsync <SiteModel>(SqlQuery.SITE_GET_BY_CODE, new
                    {
                        request.SiteCode
                    });

                    if (checkValidSiteCode.IsSuccess)
                    {
                        if (checkValidSiteCode.Data != null)
                        {
                            return(await Fail <bool>(ErrorEnum.SiteCodeHasExist.GetStringValue()));
                        }

                        var checkValidSiteName = await ReadOnlyRepository.QueryFirstOrDefaultAsync <SiteModel>(SqlQuery.SITE_GET_BY_NAME, new
                        {
                            request.SiteName
                        });

                        if (checkValidSiteName.IsSuccess)
                        {
                            if (checkValidSiteName.Data != null)
                            {
                                return(await Fail <bool>(ErrorEnum.SiteNameHasExist.GetStringValue()));
                            }

                            return(await Ok(true));
                        }
                    }
                }

                return(await Fail <bool>(checkValidId.Message));
            }
            catch (Exception exception)
            {
                return(await Fail <bool>(exception));
            }
        }
Пример #17
0
        private async Task <TResponse <bool> > CanChangePassword(int userId,
                                                                 string password)
        {
            try
            {
                var user = await ReadOnlyRepository.QueryFirstOrDefaultAsync <UserModel>(SqlQuery.USER_FIND_BY_ID,
                                                                                         new
                {
                    Id = userId
                });

                if (user.IsSuccess)
                {
                    if (user.Data != null)
                    {
                        password = Sha512(password);
                        var checkPassword = await ReadOnlyRepository.QueryFirstOrDefaultAsync <int>(SqlQuery.CHECK_PASSWORD,
                                                                                                    new
                        {
                            Id       = userId,
                            Password = password
                        });

                        if (checkPassword.IsSuccess)
                        {
                            if (checkPassword.Data > 0)
                            {
                                return(await Ok(true));
                            }

                            return(await Fail <bool>(ErrorEnum.OLD_PASSWORD_IS_INVALID.GetStringValue()));
                        }

                        return(await Fail <bool>(checkPassword.Message));
                    }

                    return(await Fail <bool>(ErrorEnum.USER_HAS_NOT_EXIST.GetStringValue()));
                }

                return(await Fail <bool>(user.Message));
            }
            catch (Exception exception)
            {
                return(await Fail <bool>(exception));
            }
        }
Пример #18
0
        public async Task <TResponse <LogOnResponse> > LogOn(LogOnModel request,
                                                             string userAgent,
                                                             string ipAddress)
        {
            try
            {
                string password = Sha512(request.Password);
                var    result   = await ReadOnlyRepository.QueryFirstOrDefaultAsync <LogOnResponse>(SqlQuery.USER_LOGON,
                                                                                                    new
                {
                    request.Username,
                    Password = password
                });

                if (result.IsSuccess)
                {
                    if (result.Data != null)
                    {
                        result.Data.Token = new AuthenticationModule().GenerateTokenForUser(result.Data.Id,
                                                                                            request.Username,
                                                                                            _secretKey);
                        KafkaHelper.PublishMessage(Constants.KAFKA_URL_SERVER,
                                                   Constants.TOPIC_LOG_ON,
                                                   new LogonKafkaMessage
                        {
                            UserId    = result.Data.Id,
                            UserAgent = userAgent,
                            IpAddress = ipAddress,
                            DateTime  = DateTime.Now
                        });
                        return(await Ok(result.Data));
                    }

                    return(await Fail <LogOnResponse>(ErrorEnum.AUTHENTICATION_WRONG.GetStringValue()));
                }

                return(await Fail <LogOnResponse>(result.Message));
            }
            catch (Exception exception)
            {
                return(await Fail <LogOnResponse>(exception));
            }
        }
Пример #19
0
        public async Task <TResponse <int> > GetIdByCrmId(int crmId)
        {
            try
            {
                var result = await ReadOnlyRepository.QueryFirstOrDefaultAsync <int>(SqlQuery.CRM_STATUS_GET_ID_BY_CRM_ID,
                                                                                     new
                {
                    Id = crmId
                });

                if (result.IsSuccess)
                {
                    return(await Ok(result.Data));
                }

                return(await Fail <int>(result.Message));
            }
            catch (Exception exception)
            {
                return(await Fail <int>(exception));
            }
        }
Пример #20
0
        public async Task <TResponse <RoleModel> > GetById(int userId,
                                                           int id,
                                                           int permissionId)
        {
            try
            {
                var checkValid = await _userService.CheckPermission(userId,
                                                                    permissionId);

                if (checkValid.IsSuccess)
                {
                    var result = await ReadOnlyRepository.QueryFirstOrDefaultAsync <RoleModel>(SqlQuery.ROLE_GET_BY_ID,
                                                                                               new
                    {
                        Id = id
                    });

                    if (result.IsSuccess)
                    {
                        if (result.Data != null)
                        {
                            return(await Ok(result.Data));
                        }

                        return(await Fail <RoleModel>(ErrorEnum.ROLE_HAS_NOT_EXIST.GetStringValue()));
                    }

                    return(await Fail <RoleModel>(result.Message));
                }

                return(await Fail <RoleModel>(checkValid.Message));
            }
            catch (Exception exception)
            {
                return(await Fail <RoleModel>(exception));
            }
        }
Пример #21
0
        /// <summary>
        ///     Add User
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        public async Task <TResponse <bool> > Add(int userId,
                                                  InsertUserModel user)
        {
            try
            {
                var canInsert = await CanInsert(user);

                if (canInsert.IsSuccess)
                {
                    var result = await WriteRepository.ExecuteScalarAsync <int>(SqlQuery.USER_INSERT, new
                    {
                        user.Username,
                        user.Email,
                        user.Name,
                        user.Phone,
                        Password    = Sha512(user.Password),
                        UserCreated = userId,
                        UserUpdated = userId
                    });

                    if (result != null)
                    {
                        if (result.IsSuccess)
                        {
                            if (result.Data == 0)
                            {
                                return(await Fail <bool>($"Insert USER {user.Username} is failure"));
                            }

                            var insertedUserId = result.Data;

                            #region Insert site

                            if (user.Sites != null &&
                                user.Sites.Any())
                            {
                                foreach (var siteId in user.Sites)
                                {
                                    var site = await ReadOnlyRepository.QueryFirstOrDefaultAsync <SiteModel>(SqlQuery.SITE_GET_BY_ID, new
                                    {
                                        Id = siteId
                                    });

                                    if (site.IsSuccess &&
                                        site.Data != null)
                                    {
                                        await WriteRepository.ExecuteAsync(SqlQuery.USER_SITE_INSERT, new
                                        {
                                            UserId      = insertedUserId,
                                            SiteId      = siteId,
                                            UserCreated = userId,
                                            UserUpdated = userId
                                        });
                                    }
                                }
                            }

                            #endregion

                            #region Insert role

                            if (user.Roles != null &&
                                user.Roles.Any())
                            {
                                foreach (var roleId in user.Roles)
                                {
                                    var role = await ReadOnlyRepository.QueryFirstOrDefaultAsync <RoleModel>(SqlQuery.ROLE_GET_BY_ID, new
                                    {
                                        Id = roleId
                                    });

                                    if (role.IsSuccess &&
                                        role.Data != null)
                                    {
                                        await WriteRepository.ExecuteAsync(SqlQuery.ROLE_GROUP_INSERT_USER, new
                                        {
                                            UserId      = insertedUserId,
                                            RoleId      = roleId,
                                            UserCreated = userId,
                                            UserUpdated = userId
                                        });
                                    }
                                }
                            }

                            #endregion

                            return(await Ok(true));
                        }

                        return(await Fail <bool>(result.Message));
                    }

                    return(await Fail <bool>($"Insert USER {user.Username} is failure"));
                }

                return(await Fail <bool>(canInsert.Message));
            }
            catch (Exception exception)
            {
                return(await Fail <bool>(exception));
            }
        }
Пример #22
0
        /// <summary>
        ///     Update User
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        public async Task <TResponse <bool> > Update(int userId,
                                                     UpdateUserModel user)
        {
            try
            {
                var canUpdate = await CanUpdate(user);

                if (canUpdate.IsSuccess)
                {
                    var result = await WriteRepository.ExecuteAsync(SqlQuery.USER_UPDATE, new
                    {
                        user.Id,
                        user.Email,
                        user.Name,
                        user.Phone,
                        UserUpdated = userId
                    });

                    if (result != null)
                    {
                        if (result.IsSuccess)
                        {
                            if (result.Data == 0)
                            {
                                return(await Fail <bool>($"Update USER {user.Id} is failure"));
                            }

                            #region Set Sites

                            var sites = user.Sites ?? new List <int>();

                            var oldSites = await ReadOnlyRepository.QueryAsync <SiteModel>(SqlQuery.SITE_GET_BY_USER_ID, new
                            {
                                UserId = user.Id
                            });

                            //kiem tra site nao chua ton tai thi them moi
                            foreach (var site in sites)
                            {
                                var exist = await ReadOnlyRepository.QueryFirstOrDefaultAsync <int>(SqlQuery.USER_CHECK_SITE_VALID, new
                                {
                                    UserId = user.Id,
                                    SiteId = site
                                });

                                if (exist.IsSuccess)
                                {
                                    if (exist.Data == 0)
                                    {
                                        //chua co, them moi vao
                                        var siteId = await ReadOnlyRepository.QueryFirstOrDefaultAsync <SiteModel>(SqlQuery.SITE_GET_BY_ID, new
                                        {
                                            Id = site
                                        });

                                        if (siteId.IsSuccess &&
                                            siteId.Data != null)
                                        {
                                            await WriteRepository.ExecuteAsync(SqlQuery.USER_SITE_INSERT, new
                                            {
                                                UserId      = user.Id,
                                                SiteId      = siteId.Data.Id,
                                                UserCreated = userId,
                                                UserUpdated = userId
                                            });
                                        }
                                    }
                                }
                            }

                            //kiem tra site nao bi xoa bo
                            if (oldSites.IsSuccess)
                            {
                                if (oldSites.Data != null &&
                                    oldSites.Data.Any())
                                {
                                    foreach (var oldSite in oldSites.Data)
                                    {
                                        if (!sites.Contains(oldSite.Id))
                                        {
                                            await WriteRepository.ExecuteAsync(SqlQuery.REMOVE_USER_FROM_SITE, new
                                            {
                                                UserId      = user.Id,
                                                SiteId      = oldSite.Id,
                                                UserUpdated = userId
                                            });
                                        }
                                    }
                                }
                            }

                            #endregion

                            #region Set Roles

                            var roles = user.Roles ?? new List <int>();

                            var oldRoles = await ReadOnlyRepository.QueryAsync <RoleModel>(SqlQuery.ROLE_GET_BY_USER_ID, new
                            {
                                UserId = user.Id
                            });

                            //kiem tra role nao chua ton tai thi them moi
                            foreach (var role in roles)
                            {
                                var exist = await ReadOnlyRepository.QueryFirstOrDefaultAsync <int>(SqlQuery.USER_CHECK_ROLE_VALID, new
                                {
                                    UserId = user.Id,
                                    RoleId = role
                                });

                                if (exist.IsSuccess)
                                {
                                    if (exist.Data == 0)
                                    {
                                        //chua co, them moi vao
                                        var roleId = await ReadOnlyRepository.QueryFirstOrDefaultAsync <RoleModel>(SqlQuery.ROLE_GET_BY_ID, new
                                        {
                                            Id = role
                                        });

                                        if (roleId.IsSuccess &&
                                            roleId.Data != null)
                                        {
                                            await WriteRepository.ExecuteAsync(SqlQuery.USER_ROLE_INSERT, new
                                            {
                                                UserId      = user.Id,
                                                RoleId      = roleId.Data.Id,
                                                UserCreated = userId,
                                                UserUpdated = userId
                                            });
                                        }
                                    }
                                }
                            }

                            //kiem tra role nao bi xoa bo
                            if (oldRoles.IsSuccess)
                            {
                                if (oldRoles.Data != null &&
                                    oldRoles.Data.Any())
                                {
                                    foreach (var oldRole in oldRoles.Data)
                                    {
                                        if (!roles.Contains(oldRole.Id))
                                        {
                                            await WriteRepository.ExecuteAsync(SqlQuery.REMOVE_USER_FROM_ROLE, new
                                            {
                                                UserId      = user.Id,
                                                RoleId      = oldRole.Id,
                                                UserUpdated = userId
                                            });
                                        }
                                    }
                                }
                            }

                            #endregion

                            return(await Ok(true));
                        }

                        return(await Fail <bool>(result.Message));
                    }

                    return(await Fail <bool>($"Update USER {user.Id} is failure"));
                }

                return(await Fail <bool>(canUpdate.Message));
            }
            catch (Exception exception)
            {
                return(await Fail <bool>(exception));
            }
        }
        public async Task <TResponse <ApiResourceDto> > GetByName(string name)
        {
            try
            {
                var result = await ReadOnlyRepository.QueryFirstOrDefaultAsync <ApiResourceDto>(SqlQuery.API_RESOURCE_GET_BY_NAME, new
                {
                    Name = name
                });

                if (result.IsSuccess)
                {
                    if (result.Data != null)
                    {
                        var apiResource = result.Data;

                        #region Claim

                        apiResource.UserClaims = new List <string>();
                        var claims = await ReadOnlyRepository.QueryAsync <string>(SqlQuery.API_RESOURCE_CLAIM_GET_BY_RESOURCE_ID, new
                        {
                            ApiResourceId = apiResource.Id
                        });

                        if (claims.IsSuccess &&
                            claims.Data != null)
                        {
                            apiResource.UserClaims.AddRange(claims.Data);
                        }

                        #endregion

                        #region Secrets

                        apiResource.Secrets = new List <ApiSecretDto>();
                        var secrets = await ReadOnlyRepository.QueryAsync <ApiSecretDto>(SqlQuery.API_SECRET_GET_BY_RESOURCE_ID, new
                        {
                            ApiResourceId = apiResource.Id
                        });

                        if (secrets.IsSuccess &&
                            secrets.Data != null &&
                            secrets.Data.Any())
                        {
                            apiResource.Secrets.AddRange(secrets.Data);
                        }

                        #endregion

                        #region Scopes

                        apiResource.Scopes = new List <ApiScopeDto>();
                        var scopes = await ReadOnlyRepository.QueryAsync <ApiScopeDto>(SqlQuery.API_SCOPE_GET_BY_RESOURCE_ID, new
                        {
                            ApiResourceId = apiResource.Id
                        });

                        if (scopes.IsSuccess &&
                            scopes.Data != null &&
                            scopes.Data.Any())
                        {
                            foreach (var scope in scopes.Data)
                            {
                                scope.UserClaims = new List <string>();
                                var scopeClaims = await ReadOnlyRepository.QueryAsync <string>(SqlQuery.API_SCOPE_CLAIM_GET_BY_API_SCOPE_ID, new
                                {
                                    ApiScopeId = scope.Id
                                });

                                if (scopeClaims.IsSuccess &&
                                    scopeClaims.Data != null &&
                                    scopeClaims.Data.Any())
                                {
                                    scope.UserClaims.AddRange(scopeClaims.Data);
                                }
                            }

                            apiResource.Scopes.AddRange(scopes.Data);
                        }

                        #endregion

                        return(await Ok(apiResource));
                    }

                    return(await Fail <ApiResourceDto>(ErrorEnum.ApiResourceHasNotExist.GetStringValue()));
                }

                return(await Fail <ApiResourceDto>(result.Message));
            }
            catch (Exception exception)
            {
                return(await Fail <ApiResourceDto>(exception));
            }
        }
        public async Task <TResponse <ClientDto> > GetByClientId(string clientId)
        {
            try
            {
                var result = await ReadOnlyRepository.QueryFirstOrDefaultAsync <ClientDto>(SqlQuery.CLIENT_GET_BY_CLIENT_ID, new
                {
                    ClientId = clientId
                });

                if (result.IsSuccess)
                {
                    if (result.Data != null)
                    {
                        var client = result.Data;

                        #region Set CorsOrigins

                        var corsOrigins = await GetCorsOrigin(client.Id);

                        if (corsOrigins.IsSuccess &&
                            corsOrigins.Data != null)
                        {
                            client.AllowedCorsOrigins.AddRange(corsOrigins.Data);
                        }

                        #endregion

                        #region Set GrantTypes

                        var grantTypes = await GetGrantType(client.Id);

                        if (grantTypes.IsSuccess &&
                            grantTypes.Data != null)
                        {
                            client.AllowedGrantTypes.AddRange(grantTypes.Data);
                        }

                        #endregion

                        #region Set Scopes

                        var scopes = await GetScope(client.Id);

                        if (scopes.IsSuccess &&
                            scopes.Data != null)
                        {
                            client.AllowedScopes.AddRange(scopes.Data);
                        }

                        #endregion

                        #region Set Provider

                        var providers = await GetProviders(client.Id);

                        if (providers.IsSuccess &&
                            providers.Data != null)
                        {
                            client.IdentityProviderRestrictions.AddRange(providers.Data);
                        }

                        #endregion

                        #region Set Logout Uris

                        var logoutUris = await GetLogoutRedirectUris(client.Id);

                        if (logoutUris.IsSuccess &&
                            logoutUris.Data != null)
                        {
                            client.PostLogoutRedirectUris.AddRange(logoutUris.Data);
                        }

                        #endregion

                        #region Set Redirect Uris

                        var redirectUris = await GetRedirectUris(client.Id);

                        if (redirectUris.IsSuccess &&
                            redirectUris.Data != null)
                        {
                            client.RedirectUris.AddRange(redirectUris.Data);
                        }

                        #endregion

                        #region Set Client Claims

                        var clientClaims = await GetClientClaims(client.Id);

                        if (clientClaims.IsSuccess &&
                            clientClaims.Data != null)
                        {
                            client.Claims.AddRange(clientClaims.Data);
                        }

                        #endregion

                        #region Set Client Secrets

                        var clientSecrets = await GetClientSecrets(client.Id);

                        if (clientSecrets.IsSuccess &&
                            clientSecrets.Data != null)
                        {
                            client.ClientSecrets.AddRange(clientSecrets.Data);
                        }

                        #endregion Set Client Properties

                        #region Set Client Properties

                        var clientProperties = await GetClientProperties(client.Id);

                        if (clientProperties.IsSuccess &&
                            clientProperties.Data != null &&
                            clientProperties.Data.Any())
                        {
                            foreach (var clientProperty in clientProperties.Data)
                            {
                                client.Properties.TryAdd(clientProperty.Type, clientProperty.Value);
                            }
                        }

                        #endregion

                        return(await Ok(client));
                    }

                    return(await Fail <ClientDto>(ErrorEnum.ClientHasNotExist.GetStringValue()));
                }

                return(await Fail <ClientDto>(result.Message));
            }
            catch (Exception exception)
            {
                return(await Fail <ClientDto>(exception));
            }
        }
Пример #25
0
        private async Task <TResponse <string> > CanForgotPassword(string username)
        {
            try
            {
                var findByUsername = await ReadOnlyRepository.QueryFirstOrDefaultAsync <int>(SqlQuery.USER_FIND_BY_USERNAME,
                                                                                             new
                {
                    Username = username
                });

                if (findByUsername.IsSuccess)
                {
                    if (findByUsername.Data > 0)
                    {
                        var email = await ReadOnlyRepository.QueryFirstOrDefaultAsync <string>(SqlQuery.USER_FIND_EMAIL_BY_ID,
                                                                                               new
                        {
                            Id = findByUsername.Data
                        });

                        if (email.IsSuccess)
                        {
                            if (!string.IsNullOrEmpty(email.Data))
                            {
                                return(await Ok(email.Data));
                            }

                            return(await Fail <string>(ErrorEnum.EMAIL_IS_EMPTY.GetStringValue()));
                        }

                        return(await Fail <string>(email.Message));
                    }

                    var findByEmail = await ReadOnlyRepository.QueryFirstOrDefaultAsync <int>(SqlQuery.USER_FIND_BY_EMAIL,
                                                                                              new
                    {
                        Email = username
                    });

                    if (findByEmail.IsSuccess)
                    {
                        if (findByEmail.Data > 0)
                        {
                            var email = await ReadOnlyRepository.QueryFirstOrDefaultAsync <string>(SqlQuery.USER_FIND_EMAIL_BY_ID,
                                                                                                   new
                            {
                                Id = findByUsername.Data
                            });

                            if (email.IsSuccess)
                            {
                                if (!string.IsNullOrEmpty(email.Data))
                                {
                                    return(await Ok(email.Data));
                                }

                                return(await Fail <string>(ErrorEnum.EMAIL_IS_EMPTY.GetStringValue()));
                            }

                            return(await Fail <string>(email.Message));
                        }

                        return(await Fail <string>(ErrorEnum.USERNAME_OR_EMAIL_IS_INVALID.GetStringValue()));
                    }

                    return(await Fail <string>(findByEmail.Message));
                }

                return(await Fail <string>(findByUsername.Message));
            }
            catch (Exception exception)
            {
                return(await Fail <string>(exception));
            }
        }