Example #1
0
        public void AddNewContact(int contractorId,
            string displayName,
            string name,
            string surname,
            string phone1,
            string userEmail,
            string skype)
        {
            if (contractorId <= 0 || String.IsNullOrEmpty(displayName))
                return;

            UserProfile item = new UserProfile() { DisplayName = displayName,
            Name = name, Surname = surname, Phone1 = phone1, UserEmail = userEmail, Skype = skype};

            var datacontextModel = new SkladDataContext();

            PrepareUsersData(item);
            item.ContactTypeId = 3;

            string validationMessage = ValidateUsersData(datacontextModel.UserProfiles, item, true);
            if (!String.IsNullOrEmpty(validationMessage))
            {
                logger.Error(validationMessage);
                return;
            }

            // since we are adding a new item, create a new istance
            item.NewPassword = System.Web.Security.Membership.GeneratePassword(5, 1);

            // Create membership account
            WebSecurity.CreateUserAndAccount(item.UserName, item.NewPassword);
            UserProfile newItem = (from x in datacontextModel.UserProfiles
                                where x.UserName == item.UserName
                                select x).First<UserProfile>();
            // set the new item information
            UpdateMyEmployees(newItem, item);

            Contractor contractor = datacontextModel.Contractors
              .Where(x => x.ContractorId == contractorId).FirstOrDefault<Contractor>();

            newItem.Contractors.Add(contractor);
            datacontextModel.SaveChanges();

            logger.InfoFormat("добавлен контакт {0} с паролем - {1}", item.DisplayName, item.NewPassword);
        }
Example #2
0
        private void PrepareUsersData(UserProfile profile)
        {
            profile.UserName = (profile.UserName ?? String.Empty).Trim();
            profile.DisplayName = (profile.DisplayName ?? String.Empty).Trim();
            profile.UserEmail = (profile.UserEmail ?? String.Empty).Trim();
            profile.Surname = (profile.Surname ?? String.Empty).Trim();
            profile.Name = (profile.Name ?? String.Empty).Trim();
            profile.MiddleName = (profile.MiddleName ?? String.Empty).Trim();
            profile.Phone1 = (profile.Phone1 ?? String.Empty).Trim();
            profile.Phone2 = (profile.Phone2 ?? String.Empty).Trim();
            profile.Skype = (profile.Skype ?? String.Empty).Trim();
            profile.Comment = (profile.Comment ?? String.Empty).Trim();

            if (String.IsNullOrEmpty(profile.UserName))
                profile.UserName = profile.DisplayName;

            if (String.IsNullOrEmpty(profile.DisplayName))
                profile.DisplayName = profile.UserName;

            profile.UserName = StringHelper.Transliterate(profile.UserName, true);
        }
Example #3
0
        public ActionResult MyEmployeesEditRows(UserProfile editedItem)
        {
            // Get the grid and database models
            var gridModel = new SkladJqGridModel();
            var datacontextModel = new SkladDataContext();

            // If we are in "Edit" mode
            if (gridModel.MyEmployeesGrid.AjaxCallBackMode == AjaxCallBackMode.EditRow)
            {
                if (editedItem.UserName == "admin" && !User.IsInRole("admin"))
                    return gridModel.MyEmployeesGrid.ShowEditValidationMessage("Эту запись изменить нельзя");

                PrepareUsersData(editedItem);

                string validationMessage = ValidateUsersData(datacontextModel.UserProfiles, editedItem, false);
                if (!String.IsNullOrEmpty(validationMessage))
                    return gridModel.MyEmployeesGrid.ShowEditValidationMessage(validationMessage);

                // Get the data from and find the item corresponding to the edited row
                UserProfile item = (from x in datacontextModel.UserProfiles
                                   where x.UserId == editedItem.UserId
                                   select x).First<UserProfile>();
                if (editedItem.UserId == item.UserId)
                {
                    // update the item information
                    UpdateMyEmployees(item, editedItem);

                    datacontextModel.SaveChanges();
                    UpdateUserRole(datacontextModel, item);
                    logger.InfoFormat("изменён контакт {0}", editedItem.DisplayName);

                    // Change password if need it
                    if (!String.IsNullOrEmpty(editedItem.NewPassword))
                    {
                        string resetToken = WebSecurity.GeneratePasswordResetToken(editedItem.UserName);
                        WebSecurity.ResetPassword(resetToken, editedItem.NewPassword);
                        logger.InfoFormat("изменён пароль контакта {0}", editedItem.DisplayName);
                    }
                }
            }

            if (gridModel.MyEmployeesGrid.AjaxCallBackMode == AjaxCallBackMode.AddRow)
            {
                PrepareUsersData(editedItem);
                string validationMessage = ValidateUsersData(datacontextModel.UserProfiles, editedItem, true);
                if (!String.IsNullOrEmpty(validationMessage))
                    return gridModel.MyEmployeesGrid.ShowEditValidationMessage(validationMessage);

                    // since we are adding a new item, create a new istance
                    string newPassword = editedItem.NewPassword;
                    if (String.IsNullOrEmpty(newPassword))
                        editedItem.NewPassword = System.Web.Security.Membership.GeneratePassword(5, 1);

                    // Create membership account
                    WebSecurity.CreateUserAndAccount(editedItem.UserName, editedItem.NewPassword);
                    UserProfile item = (from x in datacontextModel.UserProfiles
                                        where x.UserName == editedItem.UserName
                                        select x).First<UserProfile>();
                    // set the new item information
                    UpdateMyEmployees(item, editedItem);

                    datacontextModel.SaveChanges();
                    UpdateUserRole(datacontextModel, item);
                    logger.InfoFormat("добавлен контакт {0} с паролем - {1}", editedItem.DisplayName, editedItem.NewPassword);
            }
            if (gridModel.MyEmployeesGrid.AjaxCallBackMode == AjaxCallBackMode.DeleteRow)
            {
                UserProfile item = (from x in datacontextModel.UserProfiles
                                    where x.UserId == editedItem.UserId
                                    select x)
                               .First<UserProfile>();

                if (item.ContactTypeId == 1)
                    return gridModel.MyEmployeesGrid.ShowEditValidationMessage("Невозможно удалить сотрудника");

                // delete the record
                Membership.DeleteUser(item.UserName);
                logger.InfoFormat("удален контакт {0}", editedItem.DisplayName);
            }

            return RedirectToAction("MyEmployees", "MyCompany");
        }
Example #4
0
        public ActionResult ContactListEditRows(UserProfile editedItem)
        {
            int contractorId = 0;

            Int32.TryParse(Request.QueryString["contractorId"], out contractorId);

            if (contractorId <= 0)
                return null;

            // Get the grid and database models
            var gridModel = new SkladJqGridModel();
            var datacontextModel = new SkladDataContext();

            // If we are in "Edit" mode
            if (gridModel.MyEmployeesGrid.AjaxCallBackMode == AjaxCallBackMode.EditRow)
            {
                PrepareUsersData(editedItem);

                string validationMessage = ValidateUsersData(datacontextModel.UserProfiles, editedItem, false);
                if (!String.IsNullOrEmpty(validationMessage))
                    return gridModel.MyEmployeesGrid.ShowEditValidationMessage(validationMessage);

                // Get the data from and find the item corresponding to the edited row
                UserProfile item = (from x in datacontextModel.UserProfiles
                                    where x.UserId == editedItem.UserId
                                    select x).First<UserProfile>();

                if (item.ContactTypeId != 3)
                    return null;

                // update the item information
                UpdateMyEmployees(item, editedItem);

                datacontextModel.SaveChanges();
                logger.InfoFormat("контакт {0} изменён", item.DisplayName);

                // Change password if need it
                if (!String.IsNullOrEmpty(editedItem.NewPassword))
                {
                    string resetToken = WebSecurity.GeneratePasswordResetToken(editedItem.UserName);
                    WebSecurity.ResetPassword(resetToken, editedItem.NewPassword);
                    logger.InfoFormat("у контакта {0} был изменён пароль", editedItem.DisplayName);
                }
            }

            if (gridModel.MyEmployeesGrid.AjaxCallBackMode == AjaxCallBackMode.DeleteRow)
            {

                UserProfile item = (from x in datacontextModel.UserProfiles.Include(x => x.Contractors)
                                    where x.UserId == editedItem.UserId
                                    select x)
                               .First<UserProfile>();

                Contractor contractor = datacontextModel.Contractors
                    .Where(x => x.ContractorId == contractorId).FirstOrDefault<Contractor>();
                item.Contractors.Remove(contractor);
                datacontextModel.SaveChanges();
            }

            return RedirectToAction("ContactList", "MyCompany", new { contractorId = contractorId });
        }
Example #5
0
        private string ValidateUsersData(DbSet<UserProfile> dbSet, UserProfile profile, bool isNew)
        {
            if (String.IsNullOrEmpty(profile.UserName))
                return "Имя пользователя обязательно";

            if (String.IsNullOrEmpty(profile.DisplayName))
                return "Отображаемое имя обязательно";

            if (isNew)
            {
                UserProfile item = dbSet.Where(x =>
                    String.Compare(x.DisplayName, profile.DisplayName, StringComparison.OrdinalIgnoreCase) == 0
                    || x.UserName == profile.UserName).FirstOrDefault<UserProfile>();

                if (item != null)
                    return "Пользователь с таким именем существует";
            }
            else
            {
                UserProfile item = (from x in dbSet
                                    where x.UserId == profile.UserId
                                    select x).First<UserProfile>();

                // if DisplayName was changed
                if (item.DisplayName.ToLower() != profile.DisplayName.ToLower())
                {
                    if ((from x in dbSet
                         where x.DisplayName == profile.DisplayName
                         select x).FirstOrDefault<UserProfile>() != null)
                        return "Пользователь с таким именем существует";
                }

                // if UserName was changed
                if (item.UserName.ToLower() != profile.UserName.ToLower())
                {
                    if ((from x in dbSet
                         where x.UserName == profile.UserName
                         select x).FirstOrDefault<UserProfile>() != null)
                        return "Пользователь с таким именем существует";
                }
            }

            if (!String.IsNullOrEmpty(profile.UserEmail))
            {
                if (!ValidationHelper.IsValidEmail(profile.UserEmail))
                    return "Неверный формат email";
            }

            return String.Empty;
        }
Example #6
0
 private void UpdateUserRole(SkladDataContext context, UserProfile item)
 {
     ContactType cType = context.ContactTypes.Where(x => x.ContactTypeId == item.ContactTypeId).FirstOrDefault();
     if (cType != null)
     {
         string[] allRoles = new string[] { "employee", "limitedemployee" };
         Roles.RemoveUserFromRoles(item.UserName, allRoles);
         if (String.Compare(cType.Name, "Сотрудник", true) == 0)
         {
             Roles.AddUserToRole(item.UserName, allRoles[0]);
         }
         else if ((String.Compare(cType.Name, "Менеджер", true) == 0) || (String.Compare(cType.Name, "Сотрудник с огр. правами", true) == 0))
         {
             Roles.AddUserToRole(item.UserName, allRoles[1]);
         }
     }
 }
Example #7
0
        private void UpdateMyEmployees(UserProfile item, UserProfile editedItem)
        {
            item.UserName = editedItem.UserName;
            item.DisplayName = editedItem.DisplayName;
            item.UserEmail = editedItem.UserEmail;
            item.Surname = editedItem.Surname;
            item.Name = editedItem.Name;
            item.MiddleName = editedItem.MiddleName;
            item.Phone1 = editedItem.Phone1;
            item.Phone2 = editedItem.Phone2;
            item.Skype = editedItem.Skype;
            item.Comment = editedItem.Comment;
            item.ContactTypeId = editedItem.ContactTypeId;

            item.LegalEntityId = editedItem.LegalEntityId;

            // update user role. one-to-one relationship
            foreach (var role in Roles.GetRolesForUser(editedItem.UserName))
                Roles.RemoveUserFromRole(editedItem.UserName, role);
        }