/// <summary> /// 删除系统账号 /// </summary> /// <param name="systemId"></param> /// <param name="uId"></param> /// <returns></returns> /// <exception cref="BIStudio.Framework.DefinedException"></exception> public void UnRegisterAccount(long systemId, string uId) { var entity = _accountRepository.Get(item => item.SystemID == systemId && item.UID == uId); if (!entity.ID.HasValue) { throw CFException.Create(OperateResult.NotFound); } try { _accountRepository.Remove(entity); return; } catch (Exception ex) { throw CFException.Create(OperateResult.Fail, ex.Message, ex); } }
/// <summary> /// 注销通行证 /// </summary> /// <param name="loginName"></param> /// <returns></returns> /// <exception cref="BIStudio.Framework.DefinedException"></exception> public void PassportUnRegister(string loginName) { var entity = _passportRepository.Get(item => item.LoginName == loginName); if (!entity.ID.HasValue) { throw CFException.Create(OperateResult.NotFound); } try { _passportRepository.Remove(entity); return; } catch (Exception ex) { throw CFException.Create(OperateResult.Fail, ex.Message, ex); } }
/// <summary> /// 注销访问令牌 /// </summary> /// <param name="systemId"></param> /// <param name="passportId"></param> /// <exception cref="BIStudio.Framework.DefinedException"></exception> public void DestroyToken(long systemId, long passportId) { var entity = _tokenRepository.Get(item => item.SystemID == systemId && item.PassportID == passportId); if (!entity.ID.HasValue) { throw CFException.Create(OperateResult.NotFound); } try { _tokenRepository.Remove(entity); return; } catch (Exception ex) { throw CFException.Create(OperateResult.Fail, ex.Message, ex); } }
/// <summary> /// 生成更新令牌 /// </summary> /// <param name="accessToken"></param> /// <returns></returns> /// <exception cref="BIStudio.Framework.DefinedException"></exception> public SYSAccessToken GeneralTocken(SYSAccessTokenDTO accessToken) { //检查更新令牌 var refreshToken = _tokenRepository.Get(item => item.RefreshToken == accessToken.refresh_token); if (refreshToken.ID == null) { throw CFException.Create(STDAccessTokenResult.ClientIDOrSecretInvalid); } //创建访问令牌 var token = new SYSAccessToken(refreshToken.SystemID.Value) { uid = refreshToken.PassportID, scope = refreshToken.Scope, }; return(token); }
/// <summary> /// 使用授权Token登录 /// </summary> /// <param name="authorizeLogin"></param> /// <exception cref="BIStudio.Framework.DefinedException"></exception> public void AuthorizeLogin(SYSAuthorizeLoginDTO authorizeLogin) { //检查请求参数 if (string.IsNullOrEmpty(authorizeLogin.code)) { throw CFException.Create(STDAuthorizeLoginResult.AuthorizeCodeInvalid); } if (string.IsNullOrEmpty(authorizeLogin.username) || string.IsNullOrEmpty(authorizeLogin.password)) { throw CFException.Create(STDAuthorizeLoginResult.AccountOrPasswordInvalid); } try { //验证授权码 var authorize = CacheService.Default.Get <SYSAuthorizeDTO>("STDAuthorizeDTO_" + authorizeLogin.code); if (authorize.Equals(default(SYSAuthorizeDTO))) { throw CFException.Create(STDAuthorizeLoginResult.AuthorizeCodeInvalid); } //验证用户账号 SYSPassport passport; if (!Login(authorizeLogin.username, authorizeLogin.password, out passport)) { throw CFException.Create(STDAuthorizeLoginResult.AccountOrPasswordInvalid); } CacheService.Default.Add("STDAuthorizeDTO_" + authorizeLogin.code, new SYSAuthorizeDTO { response_type = authorize.response_type, client_id = authorize.client_id, redirect_uri = authorize.redirect_uri, scope = authorize.scope, state = authorize.state, uid = passport.ID, }); return; } catch (Exception ex) { throw CFException.Create(STDAuthorizeLoginResult.Fail, ex.Message, ex); } }
/// <summary> /// 更新密码锁定状态 /// </summary> /// <param name="loginName"></param> /// <param name="isLocked"></param> /// <returns></returns> /// <exception cref="BIStudio.Framework.DefinedException"></exception> public void PassportLock(string loginName, bool isLocked) { var passport = _passportRepository.Get(item => item.LoginName == loginName); if (!passport.ID.HasValue) { throw CFException.Create(OperateResult.NotFound); } passport.IsLocked = isLocked; try { _passportRepository.Modify(passport); return; } catch (Exception ex) { throw CFException.Create(OperateResult.Fail, ex.Message, ex); } }
internal SYSStrategy StrategyInject(SYSStrategyDTO dto) { if (string.IsNullOrEmpty(dto.Code) || string.IsNullOrEmpty(dto.Name)) { throw CFException.Create(SYSStrategyResult.NameOrCodeNotFound); } try { var tagService = TagService.GetInstance(); tagService.DependOn(this.Context); SYSTag tagInfo = tagService.GetTag(dto.ResourceTagID); if (tagInfo.ID == null) { throw CFException.Create(SYSStrategyResult.ResourceTagInvalid); } SYSTagClass tagClassInfo = tagService.GetTagClass(tagInfo.TagClassID ?? 0); if (tagClassInfo.ID == null) { throw CFException.Create(SYSStrategyResult.ResourceTagInvalid); } var prevCertificate = this.Get(new EntitySpec <SYSStrategy>(query => { query.SystemID = dto.SystemId; query.StrategyCode = dto.Code; })); if (prevCertificate.ID.HasValue) { throw CFException.Create(SYSStrategyResult.CodeAlreadyExists); } SYSStrategy entity = dto.Map <SYSStrategyDTO, SYSStrategy>(); entity.SystemID = dto.SystemId; entity.InputTime = DateTime.Now; entity.Inputer = CFContext.User.UserName; entity.InputerID = CFContext.User.ID; this.Add(entity); return(entity); } catch (Exception ex) { throw CFException.Create(SYSStrategyResult.Fail, ex); } }
/// <summary> /// 更改通行证密码 /// </summary> /// <param name="changePassword"></param> /// <returns></returns> /// <exception cref="BIStudio.Framework.DefinedException"></exception> public void PassportChangePassword(SYSPassportChangePasswordDTO dto) { if (string.IsNullOrEmpty(dto.LoginName)) { throw CFException.Create(SYSPassportChangePasswordResult.LoginNameDoesNotExist); } try { var passport = _passportRepository.Get(item => item.LoginName == dto.LoginName); //检查用户信息 if (!passport.ID.HasValue) { throw CFException.Create(SYSPassportChangePasswordResult.LoginNameDoesNotExist); } //检查密码 if (string.IsNullOrEmpty(dto.OldPassword) || (ALEncrypt.InstanceKey != dto.OldPassword && BitConverter.ToString(passport.Password) != BitConverter.ToString(passport.ComputePassword(dto.LoginName, dto.OldPassword)))) { throw CFException.Create(SYSPassportChangePasswordResult.PasswordIncorrect); } //检查新密码 if (string.IsNullOrEmpty(dto.NewPassword) || !ALValidator.IsLengthStr(dto.NewPassword, 6, 16)) { throw CFException.Create(SYSPassportChangePasswordResult.PasswordTooWeak); } if (string.IsNullOrEmpty(dto.ReNewPassword) || dto.NewPassword != dto.ReNewPassword) { throw CFException.Create(SYSPassportChangePasswordResult.RePasswordIncorrect); } //更改密码 passport.Password = passport.ComputePassword(dto.LoginName, dto.NewPassword); _passportRepository.Modify(passport); return; } catch (Exception ex) { throw CFException.Create(SYSPassportChangePasswordResult.Fail, ex.Message, ex); } }
public SYSStrategyOperation OperationAssign(SYSStrategyOperationAssignDTO dto) { if (string.IsNullOrEmpty(dto.StrategyCode) || string.IsNullOrEmpty(dto.OperationCode)) { throw CFException.Create(SYSStrategyOperationAssignResult.SystemCodeInvalid); } try { var operation = this.Context.Resolve <SYSOperationRepository>().Get(new EntitySpec <SYSOperation>(query => { query.SystemID = dto.SystemId; query.OperationCode = dto.OperationCode; })); if (!operation.ID.HasValue) { throw CFException.Create(SYSStrategyOperationAssignResult.OperationNotFound); } var strategy = this.Context.Resolve <SYSStrategyRepository>().Get(new EntitySpec <SYSStrategy>(query => { query.SystemID = dto.SystemId; query.StrategyCode = dto.StrategyCode; })); if (!strategy.ID.HasValue) { throw CFException.Create(SYSStrategyOperationAssignResult.StrategyNotFound); } var strategyGroup = this.Get(new EntitySpec <SYSStrategyOperation>(query => { query.OperationID = operation.ID; query.StrategyID = strategy.ID; })); if (strategyGroup.ID.HasValue) { throw CFException.Create(SYSStrategyOperationAssignResult.StrategyOperationAlreadyExist); } var entity = new SYSStrategyOperation { StrategyID = strategy.ID, OperationID = operation.ID, }; this.Add(entity); return(entity); } catch (Exception ex) { throw CFException.Create(SYSStrategyOperationAssignResult.Fail, ex); } }
public SYSOperationFilter OperationAssign(SYSOperationFilterAssignDTO dto) { if (string.IsNullOrEmpty(dto.OperationCode) || string.IsNullOrEmpty(dto.OperationCode)) { throw CFException.Create(SYSOperationFilterAssignResult.SystemCodeInvalid); } try { var filter = this.Context.Resolve <SYSFilterRepository>().Get(new EntitySpec <SYSFilter>(query => { query.SystemID = dto.SystemId; query.FilterCode = dto.FilterCode; })); if (!filter.ID.HasValue) { throw CFException.Create(SYSOperationFilterAssignResult.FilterNotFound); } var operation = this.Context.Resolve <SYSOperationRepository>().Get(new EntitySpec <SYSOperation>(query => { query.SystemID = dto.SystemId; query.OperationCode = dto.OperationCode; })); if (!operation.ID.HasValue) { throw CFException.Create(SYSOperationFilterAssignResult.OperationNotFound); } var operationFilter = this.Get(new EntitySpec <SYSOperationFilter>(query => { query.FilterID = filter.ID; query.OperationID = operation.ID; })); if (operationFilter.ID.HasValue) { throw CFException.Create(SYSOperationFilterAssignResult.OperationFilterAlreadyExist); } var entity = new SYSOperationFilter { OperationID = operation.ID, FilterID = filter.ID, }; this.Add(entity); return(entity); } catch (Exception ex) { throw CFException.Create(SYSOperationFilterAssignResult.Fail, ex); } }
/// <summary> /// 通行证绑定系统账号 /// </summary> /// <param name="link"></param> /// <returns></returns> /// <exception cref="BIStudio.Framework.DefinedException"></exception> public void PassportLink(SYSPassportLinkDTO link) { if (string.IsNullOrEmpty(link.LoginName) || string.IsNullOrEmpty(link.SystemCode) || string.IsNullOrEmpty(link.UID)) { throw CFException.Create(SYSPassportLinkResult.LoginNameOrUIDNotFound); } try { var passport = _passportRepository.Get(item => item.LoginName == link.LoginName); if (passport.ID == null) { throw CFException.Create(SYSPassportLinkResult.LoginNameInvalid); } var system = _systemRepository.Get(item => item.SystemCode == link.SystemCode); if (system.ID == null) { throw CFException.Create(SYSPassportLinkResult.SystemCodeInvalid); } var account = _accountRepository.Get(item => item.UID == link.UID && item.SystemID == system.ID); if (account.ID == null || account.PassportID != null) { throw CFException.Create(SYSPassportLinkResult.UIDInvalid); } account.PassportID = passport.ID; _accountRepository.Modify(new SYSAccount { ID = account.ID, PassportID = passport.ID }); return; } catch (Exception ex) { throw CFException.Create(SYSPassportLinkResult.Fail, ex.Message); } }
/// <summary> /// 注销访问令牌 /// </summary> /// <param name="accessToken"></param> /// <exception cref="BIStudio.Framework.DefinedException"></exception> public void DestroyToken(string accessToken) { if (string.IsNullOrEmpty(accessToken)) { throw CFException.Create(OperateResult.NotFound); } var entity = _tokenRepository.Get(item => item.AccessToken == accessToken); if (!entity.ID.HasValue) { throw CFException.Create(OperateResult.NotFound); } try { _tokenRepository.Remove(entity); return; } catch (Exception ex) { throw CFException.Create(OperateResult.Fail, ex.Message, ex); } }
/// <summary> /// 忘记密码 /// </summary> /// <param name="email"></param> /// <returns></returns> /// <exception cref="BIStudio.Framework.DefinedException"></exception> public SYSPassworkForgetDTO PassportForgot(string email) { try { if (string.IsNullOrEmpty(email)) { throw CFException.Create(STDPassworkForgetResult.EmailIncorrect); } var passport = _passportRepository.Get(item => item.Email == email); if (passport == null) { throw CFException.Create(STDPassworkForgetResult.EmailIncorrect); } passport.VerificationCode = ALUtils.GetGUIDShort(); _passportRepository.Modify(passport); return(new SYSPassworkForgetDTO(email, passport.LoginName, passport.VerificationCode)); } catch (Exception ex) { throw CFException.Create(STDPassworkForgetResult.Fail, ex.Message, ex); } }
/// <summary> /// 系统注册 /// </summary> /// <param name="dto"></param> /// <exception cref="BIStudio.Framework.DefinedException"></exception> public void SystemRegist(SYSSystemRegistDTO dto) { if (string.IsNullOrEmpty(dto.SystemName) || string.IsNullOrEmpty(dto.SystemCode)) { throw CFException.Create(SYSSystemRegistResult.NameOrCodeNotFound); } try { var system = _systemRepository.Get(item => item.SystemCode == dto.SystemCode); if (system.ID.HasValue) { if (system.GetVersion() > dto.GetVersion()) { throw CFException.Create(SYSSystemRegistResult.CodeAlreadyExists); } } else { system = new SYSSystem(dto.SystemCode); } system = CFMapper.Map(dto, system); if (system.ID.HasValue) { _systemRepository.Modify(system); } else { _systemRepository.Add(system); } return; } catch (Exception ex) { throw CFException.Create(SYSSystemRegistResult.Fail, ex.Message, ex); } }
/// <summary> /// 注册系统账号 /// </summary> /// <param name="account"></param> /// <returns></returns> /// <exception cref="BIStudio.Framework.DefinedException"></exception> public SYSAccount AccountRegist(SYSAccountRegistDTO dto) { if (string.IsNullOrEmpty(dto.SystemCode) || string.IsNullOrEmpty(dto.UID)) { throw CFException.Create(SYSAccountRegistResult.UIDInvalid); } try { SYSSystem system = _systemRepository.Get(item => item.SystemCode == dto.SystemCode); if (system.ID == null) { throw CFException.Create(SYSAccountRegistResult.SystemCodeInvalid); } var prevAccount = _accountRepository.Get(item => item.SystemID == system.ID && item.UID == dto.UID); var entity = dto.Map <SYSAccountRegistDTO, SYSAccount>(); if (prevAccount.ID.HasValue) { entity.ID = prevAccount.ID; _accountRepository.Modify(entity); } else { entity.SystemID = system.ID; entity.SystemName = system.SystemName; entity.InputTime = DateTime.Now; entity.InputIP = CFContext.User.IP; _accountRepository.Add(entity); } return(entity); } catch (Exception ex) { throw CFException.Create(SYSAccountRegistResult.Fail, ex.Message, ex); } }
/// <summary> /// 为指定系统颁发新证书 /// </summary> /// <param name="dto"></param> /// <exception cref="BIStudio.Framework.DefinedException"></exception> public SYSSystemCertificate CertificateIssue(SYSSystemCertificateIssueDTO dto) { if (string.IsNullOrEmpty(dto.SystemCode) || string.IsNullOrEmpty(dto.ApiKey) || string.IsNullOrEmpty(dto.CertificateName)) { throw CFException.Create(STDCertificateIssueResult.NameOrCodeNotFound); } try { SYSSystem system = _systemRepository.Get(item => item.SystemCode == dto.SystemCode); if (system.ID == null) { throw CFException.Create(STDCertificateIssueResult.SystemCodeInvalid); } SYSSystemCertificate entity = dto.Map <SYSSystemCertificateIssueDTO, SYSSystemCertificate>(); var prevCertificate = _certificateRepository.Get(item => item.ApiKey == dto.ApiKey); if (prevCertificate.ID.HasValue) { throw CFException.Create(STDCertificateIssueResult.CodeAlreadyExists); } entity.SystemID = system.ID; entity.ApiKey = entity.ApiKey; entity.Secret = ALUtils.GetGUIDShort(); entity.IsValid = true; entity.InputTime = DateTime.Now; entity.Inputer = CFContext.User.UserName; entity.InputerID = CFContext.User.ID; _certificateRepository.Add(entity); return(entity); } catch (Exception ex) { throw CFException.Create(STDCertificateIssueResult.Fail, ex.Message, ex); } }
/// <summary> /// 通行证解绑系统账号 /// </summary> /// <param name="link"></param> /// <returns></returns> /// <exception cref="BIStudio.Framework.DefinedException"></exception> public void PassportUnlink(SYSPassportLinkDTO link) { if (string.IsNullOrEmpty(link.LoginName) || string.IsNullOrEmpty(link.SystemCode) || string.IsNullOrEmpty(link.UID)) { throw CFException.Create(SYSPassportLinkResult.LoginNameOrUIDNotFound); } try { var passport = _passportRepository.Get(item => item.LoginName == link.LoginName); if (passport.ID == null) { throw CFException.Create(SYSPassportLinkResult.LoginNameInvalid); } var system = _systemRepository.Get(item => item.SystemCode == link.SystemCode); if (system.ID == null) { throw CFException.Create(SYSPassportLinkResult.SystemCodeInvalid); } var account = _accountRepository.Get(item => item.SystemID == system.ID && item.UID == link.UID); if (account.ID == null || account.PassportID == null) { throw CFException.Create(SYSPassportLinkResult.UIDInvalid); } account.Property.IsDBNull("PassportID", true); _accountRepository.Remove(item => item.SystemID == system.ID && item.PassportID == passport.ID); return; } catch (Exception ex) { throw CFException.Create(SYSPassportLinkResult.Fail, ex.Message, ex); } }
/// <summary> /// 验证tocken /// </summary> /// <param name="dto"></param> /// <exception cref="BIStudio.Framework.DefinedException"></exception> public SYSToken VerifyAccess(SYSVerifyAccessTokenDTO dto) { try { var tocken = _tokenRepository.Get(item => item.AccessToken == dto.access_token); if (!tocken.ID.HasValue || tocken.AccessToken != dto.access_token) { throw CFException.Create(STDVerifyAccessTokenResult.AccessTockenInvalid); } if (!(tocken.Scope ?? "").Contains((dto.scope ?? ""))) { throw CFException.Create(STDVerifyAccessTokenResult.AccessTockenAccessDenied); } if (tocken.ExpiresIn.HasValue && DateTime.Now > tocken.ExpiresIn.Value) { throw CFException.Create(STDVerifyAccessTokenResult.AcceccTokenExpired); } return(tocken); } catch (Exception ex) { throw CFException.Create(STDVerifyAccessTokenResult.Fail, ex.Message, ex); } }