Exemplo n.º 1
0
 public static AclUser CreateAdmin()
 {
     using (var dataContext = new HuntingEntities())
     {
         var adminUser = dataContext.AclUsers.FirstOrDefault(item => item.Email == Settings.Default.AdminEmail);
         if (adminUser == null)
         {
             adminUser = new AclUser()
             {
                 AccountTypeEx     = AccountTypeEnum.Admin,
                 Email             = Settings.Default.AdminEmail,
                 PasswordHash      = PasswordStorage.CreateHash(Settings.Default.AdminPassword),
                 SysCreated        = DateTime.Now,
                 MaxTerritoryCount = -1,
                 Fullname          = Settings.Default.AdminFullname,
             };
             dataContext.AclUsers.Add(adminUser);
         }
         else
         {
             adminUser.AccountTypeEx = AccountTypeEnum.Admin;
             adminUser.PasswordHash  = PasswordStorage.CreateHash(Settings.Default.AdminPassword);
         }
         dataContext.SaveChanges();
         return(adminUser);
     }
 }
Exemplo n.º 2
0
        public void FillTerritoryInfo(Territory territory, AclUser aclUser)
        {
            this.MapAreaList = territory.MapAreas.Where(item => item.IsDeleted == false).Select(item => new MapAreaModel(item)).ToList();
            this.MapItemList = territory.MapItems.Where(item => item.IsDeleted == false).Select(item => new MapItemModel(item)).ToList();

            var sharedMapItems = territory.UserMapPoints.Where(item =>
                                                               item.Id != this.Id && (item.IsPublic || item.AclUserId == aclUser.Id || item.UserMapPointShares.Any(shareItem => shareItem.AclUserId == aclUser.Id))).
                                 ToList();

            this.UserMapItemList = sharedMapItems.ConvertAll(item => new MapItemModel(item, aclUser));

            for (int mapAreaIndex = 0; mapAreaIndex < this.MapAreaList.Count; mapAreaIndex++)
            {
                this.MapAreaList[mapAreaIndex].Index = mapAreaIndex;
            }
            int index = 0;

            foreach (var mapItem in this.MapItemList)
            {
                mapItem.Index = index;
                index++;
            }
            foreach (var mapItem in this.UserMapItemList)
            {
                mapItem.Index = index;
                index++;
            }
        }
Exemplo n.º 3
0
 public static RegisterResultEnum RegisterUser(HuntingEntities dataContext, RegisterModel model, out UserSession session)
 {
     session = null;
     try
     {
         var lowerEmail = model.Email.Trim().ToLower();
         var isAny      = dataContext.AclUsers.Any(item => item.Email == lowerEmail);
         if (isAny)
         {
             return(RegisterResultEnum.AlreadyUsed);
         }
         var newUser = new AclUser()
         {
             Email        = model.Email,
             PasswordHash = PasswordStorage.CreateHash(model.Password),
             SysCreated   = DateTime.Now,
         };
         dataContext.AclUsers.Add(newUser);
         var newSession = new UserSession()
         {
             AclUser    = newUser,
             SysCreated = DateTime.Now,
             Session    = Guid.NewGuid().ToString(),
         };
         dataContext.UserSessions.Add(newSession);
         dataContext.SaveChanges();
         session = newSession;
         return(RegisterResultEnum.Success);
     }
     catch (Exception exception)
     {
         logger.Error(exception, "RegisterUser");
         return(RegisterResultEnum.Error);
     }
 }
Exemplo n.º 4
0
        public static bool ChangePassword(HuntingEntities dataContext, AclUser aclUser, ChangePasswordModel model, bool isConfirm)
        {
            try
            {
                aclUser.EmailCode       = null;
                aclUser.EmailCodeExpire = null;
                aclUser.PasswordHash    = PasswordStorage.CreateHash(model.Password);
                var newSession = new UserSession()
                {
                    AclUser    = aclUser,
                    SysCreated = DateTime.Now,
                    Session    = Guid.NewGuid().ToString(),
                };
                dataContext.UserSessions.Add(newSession);
                if (isConfirm)
                {
                    foreach (var territoryUser in aclUser.TerritoryUsers)
                    {
                        if (territoryUser.UserRoleEx == TerritoryUserRoleEnum.Invited)
                        {
                            territoryUser.UserRoleEx = TerritoryUserRoleEnum.Member;
                        }
                    }
                }

                dataContext.SaveChanges();
                return(true);
            }
            catch (Exception exception)
            {
                logger.Error(exception, "ChangePassword");
                return(false);
            }
        }
 public AclUserUpdateModel(AclUser aclUser)
 {
     this.Id                = aclUser.Id;
     this.Email             = aclUser.Email;
     this.AccountType       = aclUser.AccountTypeEx;
     this.MaxTerritoryCount = aclUser.MaxTerritoryCount.ToString();
     this.Fullname          = aclUser.Fullname;
 }
 public TerritoryListItem(Territory territory, AclUser aclUser)
 {
     this.Id          = territory.Id;
     this.Name        = territory.Name;
     this.Description = territory.Description;
     this.IsSteward   = (territory.StewardId == aclUser.Id);
     this.IsEditor    = territory.TerritoryUsers.Any(item => item.UserRoleEx == TerritoryUserRoleEnum.Editor && item.AclUserId == aclUser.Id);
 }
Exemplo n.º 7
0
 public HomeViewModel(AclUser user)
 {
     this.IsUserLogged = true;
     this.StewardList  = user.Territories.ToList().ConvertAll(item => new TerritoryListItemModel(item));
     this.HunterList   = user.TerritoryUsers.ToList().ConvertAll(item => new TerritoryListItemModel(item.Territory));
     this.CanCreate    = user.CanCreateTerritory();
     this.CanContact   = (user.AccountTypeEx != AccountTypeEnum.Demo);
 }
Exemplo n.º 8
0
 public MapItemModel(UserMapPoint mapPoint, AclUser aclUser)
 {
     this.Id          = mapPoint.Id;
     this.Name        = mapPoint.Name;
     this.Description = mapPoint.Description;
     this.Coordinate  = new CoordinateModel(mapPoint.LocationX, mapPoint.LocationY);
     this.CanUpdate   = aclUser.CanUpdateUserPoint(mapPoint);
     this.ItemType    = "user-point";
 }
Exemplo n.º 9
0
 public ManageModel(AclUser aclUser)
 {
     this.Id                 = aclUser.Id;
     this.Fullname           = aclUser.Fullname;
     this.Email              = aclUser.Email;
     this.AccountType        = AccountTypeEnumConvertor.GetString(aclUser.AccountTypeEx);
     this.MaxTerritoryCount  = aclUser.MaxTerritoryCount;
     this.UsedTerritoryCount = aclUser.Territories.Where(item => item.IsDeleted == false).Count();
     this.CanChangePassword  = (aclUser.AccountTypeEx != AccountTypeEnum.Demo);
 }
Exemplo n.º 10
0
 public AclUserDetailModel(AclUser aclUser)
 {
     this.Id                = aclUser.Id;
     this.Email             = aclUser.Email;
     this.IsDisabledText    = aclUser.IsDisabled ? GlobalRes.BOOL_VALUE_TRUE : GlobalRes.BOOL_VALUE_FALSE;
     this.IsDisabled        = aclUser.IsDisabled;
     this.AccountType       = AccountTypeEnumConvertor.GetString(aclUser.AccountTypeEx);
     this.MaxTerritoryCount = aclUser.MaxTerritoryCount;
     this.Fullname          = aclUser.Fullname;
 }
Exemplo n.º 11
0
        public ActionResult UserUpdate(AclUserUpdateModel model)
        {
            try
            {
                using (var dataContext = new HuntingEntities())
                {
                    int languageId = (int)Session[LocalizationAttribute.SESSION_LANGUAGE_ID];
                    var userName   = User.Identity.Name;
                    var user       = AclUserContext.GetDetail(dataContext, userName);
                    if (user.AccountTypeEx != AccountTypeEnum.Admin)
                    {
                        return(RedirectToAction("Index", "Home"));
                    }

                    AclUser updateItem = null;
                    if (model.IsCreate == false)
                    {
                        updateItem = AclUserContext.GetDetail(dataContext, model.Id);
                        if (updateItem == null)
                        {
                            return(RedirectToAction("Index", "Admin"));
                        }
                    }
                    else
                    {
                        var isUsed = AclUserContext.IsEmailUsed(dataContext, model.Email, model.Id);
                        if (isUsed)
                        {
                            ModelState.AddModelError("Email", AdminRes.VALIDATION_EMAIL_USED);
                        }
                    }
                    if (ModelState.IsValid)
                    {
                        var itemId = AclUserContext.Update(dataContext, updateItem, model, user.Id);
                        if (itemId.HasValue)
                        {
                            return(RedirectToAction("UserDetail", "Admin", new { id = itemId.Value }));
                        }
                        ModelState.AddModelError("", GlobalRes.VALIDATION_UPDATE_FAILED);
                    }
                    model.FillCodeLists(languageId);
                    return(View(model));
                }
            }
            catch (Exception exception)
            {
                logger.Error(exception, "AdminController");
                return(RedirectToAction("Index", "Home"));
            }
        }
Exemplo n.º 12
0
 public static bool Delete(HuntingEntities dataContext, Question updateItem, AclUser user)
 {
     try
     {
         updateItem.IsDeleted  = true;
         updateItem.SysEditor  = user.Id;
         updateItem.SysUpdated = DateTime.Now;
         dataContext.SaveChanges();
         return(true);
     }
     catch (Exception exception)
     {
         logger.Error(exception, "Delete");
         return(false);
     }
 }
Exemplo n.º 13
0
 public static bool ForgotPassword(HuntingEntities dataContext, AclUser aclUser)
 {
     try
     {
         aclUser.EmailCode       = Guid.NewGuid().ToString();
         aclUser.EmailCodeExpire = DateTime.Now.AddHours(Settings.Default.EmailCodeExpireHours);
         EmailContext.CreateForgottenEmail(dataContext, aclUser);
         dataContext.SaveChanges();
         return(true);
     }
     catch (Exception exception)
     {
         logger.Error(exception, "ForgotPassword({0})", aclUser != null ? aclUser.Email : "N/A");
         return(false);
     }
 }
Exemplo n.º 14
0
 public static bool Disable(HuntingEntities dataContext, AclUser aclUser, int userId, bool isDisabled)
 {
     try
     {
         aclUser.SysEditor  = userId;
         aclUser.SysUpdated = DateTime.Now;
         aclUser.IsDisabled = isDisabled;
         dataContext.SaveChanges();
         return(true);
     }
     catch (Exception exception)
     {
         logger.Error(exception, "Disable({0})", isDisabled);
     }
     return(false);
 }
Exemplo n.º 15
0
 public static bool Delete(HuntingEntities dataContext, AclUser aclUser, int userId)
 {
     try
     {
         aclUser.SysEditor  = userId;
         aclUser.SysUpdated = DateTime.Now;
         aclUser.IsDeleted  = true;
         dataContext.SaveChanges();
         return(true);
     }
     catch (Exception exception)
     {
         logger.Error(exception, "Delete");
     }
     return(false);
 }
Exemplo n.º 16
0
 public static List <Territory> GetListForUser(HuntingEntities dataContext, AclUser user)
 {
     try
     {
         var territoryList = dataContext.Territories.
                             Where(item => item.IsDeleted == false && (item.StewardId == user.Id || item.TerritoryUsers.
                                                                       Any(territoryUser => territoryUser.AclUserId == user.Id))).
                             ToList();
         return(territoryList);
     }
     catch (Exception exception)
     {
         logger.Error(exception, "GetListForUser");
         return(null);
     }
 }
Exemplo n.º 17
0
        public static bool InviteUser(HuntingEntities dataContext, Territory territory, string email, string name, AclUser sender)
        {
            var isNewUser     = false;
            var lowerUserName = email.Trim().ToLower();

            var aclUser = dataContext.AclUsers.FirstOrDefault(item => item.IsDeleted == false && item.Email == lowerUserName);

            if (aclUser == null)
            {
                if (string.IsNullOrWhiteSpace(name))
                {
                    return(false);
                }
                aclUser = new AclUser()
                {
                    AccountTypeEx     = AccountTypeEnum.Standard,
                    Email             = email,
                    EmailCode         = Guid.NewGuid().ToString(),
                    Fullname          = name,
                    MaxTerritoryCount = 0,
                    SysCreated        = DateTime.Now,
                    SysEditor         = sender.Id,
                    SysUpdated        = DateTime.Now,
                };
                dataContext.AclUsers.Add(aclUser);
                EmailContext.CreateInviteEmail(dataContext, aclUser, sender);

                isNewUser = true;
            }

            if (territory.TerritoryUsers.Any(item => item.AclUserId == aclUser.Id))
            {
                return(true);
            }

            var territoryUser = new TerritoryUser()
            {
                AclUser    = aclUser,
                UserRoleEx = isNewUser ? TerritoryUserRoleEnum.Invited : TerritoryUserRoleEnum.Member,
            };

            territory.TerritoryUsers.Add(territoryUser);
            dataContext.SaveChanges();
            return(true);
        }
Exemplo n.º 18
0
        private MailMessage DeleteRequestCoordinatorEmail(Request request, AclUser user)
        {
            var subject = string.Format("Заявка №{0} {1} была удалена",
                                        request.IdRequest, request.RequestType.Name.ToLower());
            var body = string.Format("Здравствуйте, {0}!<br>{1}.", user.Snp, subject);

            body += GetRequestDescriptionPart(request);
            var message = new MailMessage
            {
                IsBodyHtml = true,
                From       = _from,
                Subject    = subject,
                Body       = body
            };

            message.To.Add(new MailAddress(user.Email));
            return(message);
        }
Exemplo n.º 19
0
        public static void CreateInviteEmail(HuntingEntities dataContext, AclUser aclUser, AclUser sender)
        {
            var template = TemplateContext.LoadTemplate(INVITE_USER_TEMPLATE);

            if (template == null)
            {
                return;
            }
            var email = new EmailInfo();

            email.SysCreated      = DateTime.Now;
            email.SysCreator      = sender.Id;
            email.ReceiverAddress = aclUser.Email;
            email.SendStateEx     = SendStateEnum.InSending;
            email.Subject         = template.Title;
            email.Message         = string.Format(template.TemplateContent,
                                                  ContextUtils.FullyQualifiedApplicationPath, aclUser.EmailCode, sender.Fullname);
            dataContext.EmailInfoes.Add(email);
        }
Exemplo n.º 20
0
 public static int?Update(HuntingEntities dataContext, AclUser updateItem, AclUserUpdateModel model, int userId)
 {
     try
     {
         if (model.IsCreate)
         {
             updateItem = new AclUser()
             {
                 SysCreated = DateTime.Now,
                 EmailCode  = Guid.NewGuid().ToString(),
             };
             dataContext.AclUsers.Add(updateItem);
         }
         updateItem.Email         = model.Email;
         updateItem.AccountTypeEx = model.AccountType;
         updateItem.Fullname      = model.Fullname;
         if (updateItem.AccountTypeEx == AccountTypeEnum.Admin)
         {
             updateItem.MaxTerritoryCount = -1;
         }
         else if (updateItem.AccountTypeEx == AccountTypeEnum.Standard)
         {
             updateItem.MaxTerritoryCount = 0;
         }
         else
         {
             updateItem.MaxTerritoryCount = int.Parse(model.MaxTerritoryCount);
         }
         updateItem.SysEditor  = userId;
         updateItem.SysUpdated = DateTime.Now;
         if (model.IsCreate)
         {
             EmailContext.CreateRegistrationEmail(dataContext, updateItem, userId);
         }
         dataContext.SaveChanges();
         return(updateItem.Id);
     }
     catch (Exception exception)
     {
         logger.Error(exception, "Update");
     }
     return(null);
 }
Exemplo n.º 21
0
        public static void CreateRegistrationEmail(HuntingEntities dataContext, AclUser aclUser, int userId)
        {
            var template = TemplateContext.LoadTemplate(CREATE_ACCOUNT_TEMPLATE);

            if (template == null)
            {
                return;
            }
            var email = new EmailInfo();

            email.SysCreated      = DateTime.Now;
            email.SysCreator      = userId;
            email.ReceiverAddress = aclUser.Email;
            email.SendStateEx     = SendStateEnum.InSending;
            email.Subject         = template.Title;
            email.Message         = string.Format(template.TemplateContent,
                                                  ContextUtils.FullyQualifiedApplicationPath, aclUser.EmailCode);
            dataContext.EmailInfoes.Add(email);
        }
Exemplo n.º 22
0
        private MailMessage SetRequestStateDispatcherEmail(Request request,
                                                           RequestStateType requestStateType,
                                                           string agreementReason, AclUser user)
        {
            var subject = string.Format("Изменен статус заявки №{0} {1}",
                                        request.IdRequest, request.RequestType.Name.ToLower());
            var body = string.Format("Здравствуйте, {0}!<br>{1} на <b>«{2}»</b>.", user.Snp, subject,
                                     RequestHelper.VerbRequestState(requestStateType.Name).ToLower());

            if (requestStateType.IdRequestStateType == 2)
            {
                if (request.RequestAgreements.Any(r => r.IdAgreementType == 2) &&
                    request.RequestAgreements.Where(r => r.IdAgreementType == 2).
                    All(r => r.IdAgreementState != 1))
                {
                    subject = string.Format("По заявке №{0} {1} завершено дополнительное согласование",
                                            request.IdRequest, request.RequestType.Name.ToLower());
                }
                else
                {
                    subject = string.Format("Поступила заявка №{0} {1}",
                                            request.IdRequest, request.RequestType.Name.ToLower());
                }
                body = string.Format("Здравствуйте, {0}!<br>{1}.", user.Snp, subject);
            }
            if (!string.IsNullOrEmpty(agreementReason) && requestStateType.IdRequestStateType == 5)
            {
                body += "<br><br><b>Причина: </b>" + agreementReason;
            }
            body += GetRequestDescriptionPart(request);
            body += GetRequestLink(request);
            var message = new MailMessage
            {
                IsBodyHtml = true,
                From       = _from,
                Subject    = subject,
                Body       = body
            };

            message.To.Add(new MailAddress(user.Email));
            return(message);
        }
Exemplo n.º 23
0
        private MailMessage UpdateRequestDispatcherEmail(Request request, AclUser user)
        {
            var subject = string.Format("Создана заявка №{0} {1}",
                                        request.IdRequest,
                                        request.RequestType.Name.ToLower());
            var body = string.Format("Здравствуйте, {0}!<br>{1}. Данная заявка является автоматически согласованной.",
                                     user.Snp, subject);

            body += GetRequestDescriptionPart(request);
            body += GetRequestLink(request);
            var message = new MailMessage
            {
                IsBodyHtml = true,
                From       = _from,
                Subject    = subject,
                Body       = body
            };

            message.To.Add(new MailAddress(user.Email));
            return(message);
        }
Exemplo n.º 24
0
 public static bool Contact(HuntingEntities dataContext, Territory territory, AclUser user, string message)
 {
     try
     {
         var contact = new TerritoryUserContact()
         {
             AclUserId  = user.Id,
             Message    = message,
             SysCreated = DateTime.Now,
             SysCreator = user.Id,
         };
         territory.TerritoryUserContacts.Add(contact);
         dataContext.SaveChanges();
         return(true);
     }
     catch (Exception exception)
     {
         logger.Error(exception, "UpdatePersonList");
         return(false);
     }
 }
Exemplo n.º 25
0
        private MailMessage CreateRequestCoordinatorEmail(Request request, AclUser user)
        {
            var subject = string.Format("Создана заявка №{0} {1}",
                                        request.IdRequest,
                                        request.RequestType.Name.ToLower());
            var body = string.Format("Здравствуйте, {0}!<br>{1}, требующая вашего согласования.",
                                     user.Snp, subject);

            body += GetRequestDescriptionPart(request);
            body += GetRequestLink(request);
            var message = new MailMessage
            {
                IsBodyHtml = true,
                From       = _from,
                Subject    = subject,
                Body       = body
            };

            message.To.Add(new MailAddress(user.Email));
            return(message);
        }
        public TerritoryDetailModel(Territory territory, AclUser user)
        {
            this.Id          = territory.Id;
            this.Name        = territory.Name;
            this.Description = territory.Description;

            var mapAreaList = territory.MapAreas.Where(item => item.IsDeleted == false).ToList();

            this.MapAreaList = mapAreaList.ConvertAll(item => new MapAreaModel(item));

            var mapItemTypeList = territory.MapItemTypes.Where(item => item.IsDeleted == false).ToList();

            this.MapItemTypeList = mapItemTypeList.ConvertAll(item => new MapItemTypeModel(item));

            var userPointList = territory.UserMapPoints.
                                Where(item => item.IsDeleted == false &&
                                      (item.AclUserId == user.Id || item.IsPublic == true || item.UserMapPointShares.Any(share => share.AclUserId == user.Id))).
                                ToList();

            this.UserPointList = userPointList.ConvertAll(item => new UserMapPointModel(item));
        }
Exemplo n.º 27
0
        public TerritoryDetailModel(Territory territory, AclUser aclUser)
        {
            this.Id              = territory.Id;
            this.Name            = territory.Name;
            this.Description     = territory.Description;
            this.MapAreaList     = territory.MapAreas.Where(item => item.IsDeleted == false).ToList().ConvertAll(item => new MapAreaModel(item));
            this.MapItemTypeList = territory.MapItemTypes.Where(item => item.IsDeleted == false).ToList().ConvertAll(item => new MapItemTypeModel(item));

            var sharedMapItems = territory.UserMapPoints.
                                 Where(item => item.IsDeleted == false && (item.IsPublic || item.AclUserId == aclUser.Id || item.UserMapPointShares.Any(shareItem => shareItem.AclUserId == aclUser.Id))).
                                 OrderBy(item => item.Name).ToList();

            this.UserMapItemList = sharedMapItems.ConvertAll(item => new MapItemModel(item, aclUser));

            this.UserLocationList = new List <MapItemModel>();
            var stewardLocation = territory.AclUserSteward.UserLocations.OrderByDescending(item => item.SysCreated).FirstOrDefault();

            if (stewardLocation != null && stewardLocation.SysCreated > DateTime.Now.AddMinutes(-Settings.Default.UserLocationLastMinutes))
            {
                this.UserLocationList.Add(new MapItemModel(stewardLocation));
            }
            foreach (var territoryUser in territory.TerritoryUsers)
            {
                var userLocation = territoryUser.AclUser.UserLocations.OrderByDescending(item => item.SysCreated).FirstOrDefault();
                if (userLocation != null && userLocation.SysCreated > DateTime.Now.AddMinutes(-Settings.Default.UserLocationLastMinutes))
                {
                    this.UserLocationList.Add(new MapItemModel(userLocation));
                }
            }
            if (territory.StewardId == aclUser.Id)
            {
                this.ContactList = territory.TerritoryUserContacts.
                                   Where(item => item.IsDeleted == false).
                                   Select(item => new TerritoryUserContactModel(item)).ToList();
            }

            PrepareMapItemIndex();
            this.CanUpdate = aclUser.CanUpdateTerritory(territory);
        }
Exemplo n.º 28
0
        public IQueryable <Department> GetUserAllowedDepartments(AclUser user = null)
        {
            if (user == null)
            {
                user = GetUserInfo();
            }
            if (user == null)
            {
                return(new List <Department>().AsQueryable());
            }
            var allowedDepartments = _securityRepository.GetUserAllowedDepartments(user.Login);

            if (allowedDepartments.Any())
            {
                return(allowedDepartments);
            }
            allowedDepartments = allowedDepartments.ToList().Concat(new[]
            {
                user.Department
            }).AsQueryable();
            return(allowedDepartments);
        }
Exemplo n.º 29
0
 public static bool LoginDemoUser(HuntingEntities dataContext, AclUser demoUser, out UserSession session)
 {
     session = null;
     try
     {
         var newSession = new UserSession()
         {
             AclUserId  = demoUser.Id,
             SysCreated = DateTime.Now,
             Session    = Guid.NewGuid().ToString(),
         };
         dataContext.UserSessions.Add(newSession);
         dataContext.SaveChanges();
         session = newSession;
         return(true);
     }
     catch (Exception exception)
     {
         logger.Error(exception, "LoginDemoUser");
         return(false);
     }
 }
 public ChangePasswordModel(AclUser aclUser)
 {
     this.Code     = aclUser.EmailCode;
     this.UserName = aclUser.Fullname;
     this.Email    = aclUser.Email;
 }