Ejemplo n.º 1
0
 private void Init()
 {
     if (_initialized)
     {
         return;
     }
     lock (Locker)
     {
         if (_initialized)
         {
             return;
         }
         _acDomain.MessageDispatcher.DispatchMessage(new MemorySetInitingEvent(this));
         _devAccountById.Clear();
         _devAccountByLoginName.Clear();
         var accounts = _acDomain.RetrieveRequiredService <IOriginalHostStateReader>().GetAllDevAccounts();
         foreach (var account in accounts)
         {
             var accountState = AccountState.Create(account);
             if (!_devAccountById.ContainsKey(account.Id))
             {
                 _devAccountById.Add(account.Id, accountState);
             }
             if (!_devAccountByLoginName.ContainsKey(account.LoginName))
             {
                 _devAccountByLoginName.Add(account.LoginName, accountState);
             }
         }
         _initialized = true;
         _acDomain.MessageDispatcher.DispatchMessage(new MemorySetInitializedEvent(this));
     }
 }
Ejemplo n.º 2
0
        public void InternalModifyTest()
        {
            var account = new Account(Guid.NewGuid())
            {
                Name = "xuefly"
            };
            var state = AccountState.Create(account);

            account.Name = "薛兴帅";

            string msg = null;

            try
            {
                var engine = new Engine()
                             .SetValue("state", state)
                             .Execute("state.InternalModify(state)");
            }
            catch (JavaScriptException e)
            {
                msg = e.Message;
            }
            Assert.AreEqual("Object has no method 'InternalModify'", msg);

            Assert.AreEqual("xuefly", state.Name);
        }
Ejemplo n.º 3
0
        public void StateTest()
        {
            var account = AccountState.Create(new Account {
                Name = "xuefly"
            });

            var engine = new Engine()
                         .SetValue("p", account)
                         .Execute("p.Name = '薛兴帅'")
            ;

            Assert.AreEqual("xuefly", account.Name);
        }
Ejemplo n.º 4
0
        private IAcSession GetAcSessionByLoginName(IAcDomain acDomain, string loginName)
        {
            if (EmptyAcDomain.SingleInstance.Equals(acDomain))
            {
                return(AcSessionState.Empty);
            }
            var storage   = acDomain.GetRequiredService <IAcSessionStorage>();
            var acSession = storage.GetData(acDomain.Config.CurrentAcSessionCacheKey) as IAcSession;

            if (acSession != null)
            {
                return(acSession);
            }
            var account = AcSessionState.AcMethod.GetAccountByLoginName(acDomain, loginName);

            if (account == null)
            {
                return(AcSessionState.Empty);
            }
            var sessionEntity = AcSessionState.AcMethod.GetAcSessionEntity(acDomain, account.Id);

            if (sessionEntity != null)
            {
                if (!sessionEntity.IsAuthenticated)
                {
                    return(AcSessionState.Empty);
                }
                acSession = new AcSessionState(acDomain, sessionEntity);
            }
            else
            {
                // 使用账户标识作为会话标识会导致一个账户只有一个会话
                // TODO:支持账户和会话的一对多,为会话级的动态责任分离做准备
                var accountState    = AccountState.Create(account);
                var identity        = new AnycmdIdentity(account.LoginName);
                var acSessionEntity = new AcSession
                {
                    Id                 = account.Id,
                    AccountId          = account.Id,
                    AuthenticationType = identity.AuthenticationType,
                    Description        = null,
                    IsAuthenticated    = identity.IsAuthenticated,
                    IsEnabled          = 1,
                    LoginName          = account.LoginName
                };
                AcSessionState.AcMethod.AddAcSession(acDomain, acSessionEntity);
                acSession = new AcSessionState(acDomain, account.Id, accountState);
            }
            storage.SetData(acDomain.Config.CurrentAcSessionCacheKey, acSession);
            return(acSession);
        }
Ejemplo n.º 5
0
            private void Handle(IAcSession acSession, Guid accountId, bool isCommand)
            {
                var         acDomain              = _set._acDomain;
                var         devAccountById        = _set._devAccountById;
                var         devAccountByLoginName = _set._devAccountByLoginName;
                var         accountRepository     = acDomain.RetrieveRequiredService <IRepository <Account> >();
                var         developerRepository   = acDomain.RetrieveRequiredService <IRepository <DeveloperId> >();
                DeveloperId entity;

                lock (Locker)
                {
                    var account = accountRepository.GetByKey(accountId);
                    if (account == null)
                    {
                        throw new ValidationException("账户不存在");
                    }
                    if (devAccountById.ContainsKey(accountId))
                    {
                        throw new ValidationException("给定标识标识的开发人员已经存在" + accountId);
                    }
                    entity = new DeveloperId
                    {
                        Id = accountId
                    };
                    try
                    {
                        var accountState = AccountState.Create(account);
                        devAccountById.Add(accountId, accountState);
                        devAccountByLoginName.Add(account.LoginName, accountState);
                        if (isCommand)
                        {
                            developerRepository.Add(entity);
                            developerRepository.Context.Commit();
                        }
                    }
                    catch
                    {
                        devAccountById.Remove(accountId);
                        devAccountByLoginName.Remove(account.LoginName);
                        developerRepository.Context.Rollback();
                        throw;
                    }
                }
                if (isCommand)
                {
                    acDomain.MessageDispatcher.DispatchMessage(new DeveloperAddedEvent(acSession, entity, isPrivate: true));
                }
            }
Ejemplo n.º 6
0
            public void Handle(DeveloperUpdatedEvent message)
            {
                var          devAccountById        = _set._devAccountById;
                var          devAccountByLoginName = _set._devAccountByLoginName;
                var          entity = message.Source as AccountBase;
                AccountState oldState;

                if (!devAccountById.TryGetValue(message.Source.Id, out oldState))
                {
                    throw new GeneralException("给定标识的用户不存在");
                }
                var newState = AccountState.Create(entity);

                devAccountById[message.Source.Id] = newState;
                if (!devAccountByLoginName.ContainsKey(newState.LoginName))
                {
                    devAccountByLoginName.Add(newState.LoginName, newState);
                    devAccountByLoginName.Remove(oldState.LoginName);
                }
                else
                {
                    devAccountByLoginName[newState.LoginName] = newState;
                }
            }
Ejemplo n.º 7
0
        private void DoSignIn(IAcDomain acDomain, Dictionary <string, object> args)
        {
            if (EmptyAcDomain.SingleInstance.Equals(acDomain))
            {
                return;
            }
            var loginName  = args.ContainsKey("loginName") ? (args["loginName"] ?? string.Empty).ToString() : string.Empty;
            var password   = args.ContainsKey("password") ? (args["password"] ?? string.Empty).ToString() : string.Empty;
            var rememberMe = args.ContainsKey("rememberMe") ? (args["rememberMe"] ?? string.Empty).ToString() : string.Empty;
            var passwordEncryptionService = acDomain.GetRequiredService <IPasswordEncryptionService>();

            if (string.IsNullOrEmpty(loginName) || string.IsNullOrEmpty(password))
            {
                throw new ValidationException("用户名和密码不能为空");
            }
            var addVisitingLogCommand = new AddVisitingLogCommand(AcSessionState.Empty)
            {
                IpAddress    = IpHelper.GetClientIp(),
                LoginName    = loginName,
                VisitedOn    = null,
                VisitOn      = DateTime.Now,
                Description  = "登录成功",
                ReasonPhrase = VisitState.LogOnFail.ToName(),
                StateCode    = (int)VisitState.LogOnFail
            };

            password = passwordEncryptionService.Encrypt(password);
            var account = AcSessionState.AcMethod.GetAccountByLoginName(acDomain, loginName);

            if (account == null)
            {
                addVisitingLogCommand.Description = "用户名错误";
                acDomain.MessageDispatcher.DispatchMessage(addVisitingLogCommand);
                throw new ValidationException(addVisitingLogCommand.Description);
            }
            else
            {
                addVisitingLogCommand.AccountId = account.Id;
            }
            if (password != account.Password)
            {
                addVisitingLogCommand.Description = "密码错误";
                acDomain.MessageDispatcher.DispatchMessage(addVisitingLogCommand);
                throw new ValidationException(addVisitingLogCommand.Description);
            }
            if (account.IsEnabled == 0)
            {
                addVisitingLogCommand.Description = "对不起,该账户已被禁用";
                acDomain.MessageDispatcher.DispatchMessage(addVisitingLogCommand);
                throw new ValidationException(addVisitingLogCommand.Description);
            }
            string       auditState = account.AuditState == null ? account.AuditState : account.AuditState.ToLower();
            CatalogState dicItem;

            if (!acDomain.CatalogSet.TryGetCatalog(auditState, out dicItem))
            {
                throw new AnycmdException("意外的字典编码" + auditState);
            }
            if (auditState == null ||
                auditState == "notaudit")
            {
                addVisitingLogCommand.Description = "对不起,该账户尚未审核";
                acDomain.MessageDispatcher.DispatchMessage(addVisitingLogCommand);
                throw new ValidationException(addVisitingLogCommand.Description);
            }
            if (auditState == "auditnotpass")
            {
                addVisitingLogCommand.Description = "对不起,该账户未通过审核";
                acDomain.MessageDispatcher.DispatchMessage(addVisitingLogCommand);
                throw new ValidationException(addVisitingLogCommand.Description);
            }
            if (account.AllowStartTime.HasValue && SystemTime.Now() < account.AllowStartTime.Value)
            {
                addVisitingLogCommand.Description = "对不起,该账户的允许登录开始时间还没到。请在" + account.AllowStartTime + "后登录";
                acDomain.MessageDispatcher.DispatchMessage(addVisitingLogCommand);
                throw new ValidationException(addVisitingLogCommand.Description);
            }
            if (account.AllowEndTime.HasValue && SystemTime.Now() > account.AllowEndTime.Value)
            {
                addVisitingLogCommand.Description = "对不起,该账户的允许登录时间已经过期";
                acDomain.MessageDispatcher.DispatchMessage(addVisitingLogCommand);
                throw new ValidationException(addVisitingLogCommand.Description);
            }
            if (account.LockEndTime.HasValue || account.LockStartTime.HasValue)
            {
                DateTime lockStartTime = account.LockStartTime ?? DateTime.MinValue;
                DateTime lockEndTime   = account.LockEndTime ?? DateTime.MaxValue;
                if (SystemTime.Now() > lockStartTime && SystemTime.Now() < lockEndTime)
                {
                    addVisitingLogCommand.Description = "对不起,该账户暂被锁定";
                    acDomain.MessageDispatcher.DispatchMessage(addVisitingLogCommand);
                    throw new ValidationException(addVisitingLogCommand.Description);
                }
            }

            if (account.PreviousLoginOn.HasValue && account.PreviousLoginOn.Value >= SystemTime.Now().AddMinutes(5))
            {
                addVisitingLogCommand.Description = "检测到您的上次登录时间在未来。这可能是因为本站点服务器的时间落后导致的,请联系管理员。";
                acDomain.MessageDispatcher.DispatchMessage(addVisitingLogCommand);
                throw new ValidationException(addVisitingLogCommand.Description);
            }
            account.PreviousLoginOn = SystemTime.Now();
            if (!account.FirstLoginOn.HasValue)
            {
                account.FirstLoginOn = SystemTime.Now();
            }
            account.LoginCount = (account.LoginCount ?? 0) + 1;
            account.IpAddress  = IpHelper.GetClientIp();

            // 使用账户标识作为会话标识会导致一个账户只有一个会话
            // TODO:支持账户和会话的一对多,为会话级的动态责任分离做准备
            var        sessionEntity = AcSessionState.AcMethod.GetAcSessionEntity(acDomain, account.Id);
            IAcSession acSession;

            if (sessionEntity != null)
            {
                acSession = new AcSessionState(acDomain, sessionEntity.Id, AccountState.Create(account));
                sessionEntity.IsAuthenticated = true;
                AcSessionState.AcMethod.UpdateAcSession(acDomain, sessionEntity);
            }
            else
            {
                var accountState    = AccountState.Create(account);
                var identity        = new AnycmdIdentity(account.LoginName);
                var acSessionEntity = new AcSession
                {
                    Id                 = account.Id,
                    AccountId          = account.Id,
                    AuthenticationType = identity.AuthenticationType,
                    Description        = null,
                    IsAuthenticated    = identity.IsAuthenticated,
                    IsEnabled          = 1,
                    LoginName          = account.LoginName
                };
                AcSessionState.AcMethod.AddAcSession(acDomain, acSessionEntity);
                acSession = new AcSessionState(acDomain, account.Id, accountState);
            }
            if (HttpContext.Current != null)
            {
                HttpContext.Current.User = acSession;
                bool createPersistentCookie = rememberMe.Equals("rememberMe", StringComparison.OrdinalIgnoreCase);
                FormsAuthentication.SetAuthCookie(account.LoginName, createPersistentCookie);
            }
            else
            {
                Thread.CurrentPrincipal = acSession;
            }
            Guid?visitingLogId = Guid.NewGuid();

            acSession.SetData("UserContext_Current_VisitingLogId", visitingLogId);
            acSession.SetData(acDomain.Config.CurrentAcSessionCacheKey, acSession);

            acDomain.EventBus.Publish(new AccountLoginedEvent(acSession, account));
            acDomain.EventBus.Commit();
            addVisitingLogCommand.StateCode    = (int)VisitState.Logged;
            addVisitingLogCommand.ReasonPhrase = VisitState.Logged.ToName();
            addVisitingLogCommand.Description  = "登录成功";
            acDomain.MessageDispatcher.DispatchMessage(addVisitingLogCommand);
        }