public void Logon(Guid?licenseGuid, LogonHistory history)
        {
            IUnitOfWorkScope scope   = null;
            Account          account = null;

            try
            {
                scope = _scopeFactory.Create();

                IsValid = true;
                _logger.Info($"Начало запроса авторизации для {licenseGuid}");
                if (!licenseGuid.HasValue)
                {
                    SetError("Не определен гуид лицензии");
                }
                else
                {
                    account = _accountRepository.GetByLicenseId(licenseGuid.Value);
                    if (account == null)
                    {
                        SetError($"Не найдена УЗ для {licenseGuid}");
                    }
                    else
                    {
                        var license = account.Licenses.FirstOrDefault(l => l.Guid == licenseGuid);
                        if (license == null)
                        {
                            SetError($"Не найдена лицензия по гуиду {licenseGuid}");
                        }
                        else if (!license.Access)
                        {
                            SetError($"Для УЗ {account.Id} заблокирован доступ для лицензии {licenseGuid}");
                        }
                    }
                }

                history.Account = account;
                _logonHistoryRepository.Save(history);

                scope.Commit();
                _logger.Info($"Завершение запроса авторизации для {licenseGuid} и УЗ {account.Return(a=>a.Id)} и логона {history.Return(h=>h.Id)}");
            }
            catch (Exception e)
            {
                SetError(e.ToString(), true);
            }
            finally
            {
                scope.Do(s => s.Dispose());
            }
        }
        public void Fail_logon_without_license_guid()
        {
            var factory    = _container.Resolve <IUnitOfWorkScopeFactory>();
            var rep        = _container.Resolve <IRepository <Account> >();
            var accountRep = _container.Resolve <IRepository <Account> >();
            var licenseRep = _container.Resolve <IRepository <License> >();
            var logonRep   = _container.Resolve <IRepository <LogonHistory> >();

            IAuthenticationProcess process = null;
            LogonHistory           logon   = null;

            using (var scope = factory.Create())
            {
                //Создаем УЗ
                var account = new Account()
                {
                    CreateDate = DateTime.Now, Email = "*****@*****.**", Guid = Guid.NewGuid()
                };
                var license1 = new License()
                {
                    TypeId = 1, Guid = Guid.NewGuid(), CreateDate = DateTime.Now, ChangeDate = DateTime.Now, Access = true, Count = 1, ApplicationId = 1, Account = account
                };
                var license2 = new License()
                {
                    TypeId = 2, Guid = Guid.NewGuid(), CreateDate = DateTime.Now, ChangeDate = DateTime.Now, Access = true, Count = 1, ApplicationId = 1, Account = account
                };
                var license3 = new License()
                {
                    TypeId = 3, Guid = Guid.NewGuid(), CreateDate = DateTime.Now, ChangeDate = DateTime.Now, Access = true, Count = 1, ApplicationId = 1, Account = account
                };

                accountRep.Add(account); licenseRep.Add(license1); licenseRep.Add(license2); licenseRep.Add(license3);
                scope.Flush();

                //Проверяем доступ
                process = BG.Domain.Authentication.Bootstrap.Configuration.Configure(_container, true);
                process.Logon(null, new LogonHistory()
                {
                    CSDBuildNumber = "123"
                });
                scope.Flush();

                logon = logonRep.FirstOrDefault();
            }

            Assert.IsTrue(process != null, "Не удалось создать процесс");
            Assert.IsTrue(!process.IsValid, process.ErrorMessage ?? "Ошибка");
            Assert.IsTrue(logon != null, "Не создался логон");
            Assert.IsTrue(!logon.AccountId.HasValue || logon.AccountId.Value <= 0, "УЗ должна отсутствовать");
        }
Beispiel #3
0
        public LogonResultForm GetUserInfo(string userid)
        {
            var     mapper  = Common.GetMapperFromSession();
            UserDao userdao = new UserDao(mapper);
            var     user    = userdao.Query(new UserQueryForm {
                Name = userid
            }).FirstOrDefault();

            if (user == null)
            {
                throw new Exception("用户:" + userid + "在系统中不存在!");
            }
            if (user.Enabled == 0)
            {
                throw new Exception("该用户已被禁用,请联系管理员!");
            }
            LogonResultForm result      = new LogonResultForm();
            UserInfoDao     userInfoDao = new UserInfoDao(mapper);
            RoleDao         roleDao     = new RoleDao(mapper);
            LogonHistoryDao historyDao  = new LogonHistoryDao(mapper);
            string          token       = Guid.NewGuid().ToString().Replace("-", "");
            var             userinfo    = userInfoDao.Query(new UserInfoQueryForm {
                ID = user.ID
            }).FirstOrDefault();
            UserEntireInfo u = new UserEntireInfo {
                User = user
            };

            if (userinfo != null)
            {
                u.UserInfo = userinfo;
            }
            u.Role = roleDao.QueryRoleByUserID(u.User.ID);
            LogonHistory history = new LogonHistory
            {
                LogonTime  = DateTime.Now,
                Token      = token,
                UserID     = user.ID,
                ActiveTime = DateTime.Now,
            };

            historyDao.Add(history);
            result.token    = token;
            result.UserInfo = userinfo;
            cache.AddItem(token, u, 30 * 60);
            MenuBLL menubll = new MenuBLL();

            result.Menu = menubll.GetCurrentUserMenu(result.token);
            return(result);
        }
Beispiel #4
0
        public void Save(LogonHistory history)
        {
            Guard.AssertNotNull(history, "Не определен логон для сохранения");

            using (var scope = _scopeFactory.Create())
            {
                history.DateTime = DateTime.Now;
                history.Host     = HttpContext.Current.GetUserHostName();
                history.IP       = HttpContext.Current.GetVisitorIPAddress(); //Processor.GetClientIP(HttpContext.Current);
                                                                              //history.Comment = string.Format("{0}; IP={1}; {2}", customComment,
                                                                              //    (HttpContext.Current.Request != null ? HttpContext.Current.Request.UserHostAddress : ""),
                                                                              //    comment);

                //history.LogonApplicationID = LogonApplication.iCRM;
                history.InternalIP = HttpContext.Current.GetUserHostAddress();

                _repository.Add(history);
                scope.Commit();
            }
        }
Beispiel #5
0
 public void UpdateLoginHistory(string emailAddress)
 {
     if (LogEvent != null)
     {
         LogEvent("User's Logon History is being Created.");
     }
     using (var db = new DamoclesEntities())
     {
         var loh  = db.LogonHistories;
         var lohe = new LogonHistory();
         lohe.LoggedOnDate = DateTime.UtcNow;
         lohe.UserId       = User.GetUserID(emailAddress);
         loh.Add(lohe);
         int rows = db.SaveChanges();
         if (rows < 1)
         {
             LogEvent(emailAddress + " Failed to Add User's Logon History Record ");
         }
     }
 }