/// <summary>
        /// Update registration
        /// </summary>
        /// <param name="request">Request</param>
        /// <returns>Result</returns>
        public virtual DataResult<User> UpdateRegistration(UserRegistrationRequest request)
        {
            Guard.IsNotNull(request, "Request");

            var result = new DataResult<User>();

            if (!CommonHelper.IsValidEmail(request.Username))
            {
                result.AddError(this.localizationService.GetResource("Common.WrongEmail"));
                return result;
            }

            var user = userService.GetByUsername(request.Username);

            // add or remove administrator role
            // set the enterprise administrator role
            var administratorRole = this.userService.GetUserRoleBySystemName(SystemUserRoleNames.Administrators);
            if (administratorRole == null)
                throw new SiteException("'" + SystemUserRoleNames.Administrators + "' role could not be loaded");

            // if administrator
            if (request.IsAdministrator)
            {
                user.UserRoles.Add(administratorRole);
            }
            else
            {
                if (user.IsInUserRole(SystemUserRoleNames.Administrators))
                    user.UserRoles.Remove(administratorRole);
            }

            // add audit history
            user.AuditHistory.Add
            (
                userActivityService.InsertActivity(SystemActivityLogTypeNames.Update,
                    user.ToString(), StateKeyManager.USER_ACTIVITY_COMMENT, request.Username)
             );

            // insert the user
            this.userService.Update(user);

            // upadate attributes
            attributeService.SaveAttribute(user, SystemUserAttributeNames.FirstName, encryptionService.AESEncrypt(request.FirstName, user));
            attributeService.SaveAttribute(user, SystemUserAttributeNames.LastName, encryptionService.AESEncrypt(request.LastName, user));

            if (!string.IsNullOrEmpty(request.Mobile))
                attributeService.SaveAttribute(user, SystemUserAttributeNames.Mobile, encryptionService.AESEncrypt(request.Mobile, user));

            result.Data = user;
            return result;
        }
Exemple #2
0
        public ActionResult Edit(UserModel model)
        {
            if (ModelState.IsValid)
            {
                var request = new UserRegistrationRequest(
                    model.Username, string.Empty);

                // set the other properties
                request.FirstName = model.FirstName;
                request.LastName = model.LastName;
                request.Mobile = model.Mobile;
                request.IsAdministrator = model.IsAdministrator;

                // register this user
                var result = registrationService.UpdateRegistration(request);
                if (result.Success)
                {
                    // add the associated branches to this user
                    var associatedOffices = model.OfficeId.ToList<Guid>();
                    attributeService.SaveAttribute<List<Guid>>(result.Data, SystemAttributeNames.AssociatedBrancOffices,
                        associatedOffices);

                    result.Data.AuditHistory.Add
                    (
                        userActivityService.InsertActivity(SystemActivityLogTypeNames.EntityOfficeAssociation,
                            PrepareOfficeAssociationModel(associatedOffices, officeService, cacheManager), StateKeyManager.USER_ACTIVITY_COMMENT, request.Username)
                    );
                    userService.Update(result.Data);

                    // return notification message
                    SuccessNotification(localizationService.GetResource("Users.Updated"));
                    return RedirectToAction(SystemRouteNames.Index);
                }
            }

            //If we got this far, something failed, redisplay form
            model.AvailableOffices = base.PrepareSelectList(officeService, cacheManager);

            var user = userService.GetById(model.RowId);
            if (user == null)
                return RedirectToAction(SystemRouteNames.Index);

            model.Mobile = encryptionService.AESDecrypt(user.GetAttribute<string>(SystemUserAttributeNames.Mobile), user);
            model.IsAdministrator = user.IsInUserRole(SystemUserRoleNames.Administrators);

            return View(model);
        }
        /// <summary>
        /// Register User
        /// </summary>
        /// <param name="request">Request</param>
        /// <returns>Result</returns>
        public virtual DataResult<User> RegisterUser(UserRegistrationRequest request)
        {
            Guard.IsNotNull(request, "Request");

            var result = new DataResult<User>();

            if (!CommonHelper.IsValidEmail(request.Username))
            {
                result.AddError(this.localizationService.GetResource("Common.WrongEmail"));
                return result;
            }

            if (String.IsNullOrWhiteSpace(request.Password))
            {
                result.AddError(this.localizationService.GetResource("Users.Fields.Password"));
                return result;
            }

            //validate unique user
            if (!this.userService.IsUnique(request.Username))
            {
                result.AddError(this.localizationService.GetResource("Users.Fields.Username.NotUnique"));
                return result;
            }

            var user = new User()
            {
                CreatedOn = DateTime.UtcNow,
                Username = request.Username,
                Password = request.Password, // we have already encrypted the password on client side
                CurrentPublishingStatus = request.CurrentPublishingStatus, // whether its an active user
            };

            //add to 'Registered' role
            var registeredRole = this.userService.GetUserRoleBySystemName(SystemUserRoleNames.Users);
            if ( registeredRole == null )
                throw new SiteException("'" + SystemUserRoleNames.Users + "' role could not be loaded");
            user.UserRoles.Add(registeredRole);

            // if administrator
            if (request.IsAdministrator)
            {
                // set the enterprise administrator role
                var administratorRole = this.userService.GetUserRoleBySystemName(SystemUserRoleNames.Administrators);
                if (administratorRole == null)
                    throw new SiteException("'" + SystemUserRoleNames.Administrators + "' role could not be loaded");
                user.UserRoles.Add(administratorRole);
            }

            // add audit history
            user.AuditHistory.Add
             (
                userActivityService.InsertActivity(SystemActivityLogTypeNames.Add,
                    user.ToString(), StateKeyManager.USER_ACTIVITY_COMMENT, request.Username)
             );

            // insert the user
            this.userService.Insert(user);

            // upadate attributes
            attributeService.SaveAttribute(user, SystemUserAttributeNames.FirstName, encryptionService.AESEncrypt(request.FirstName, user));
            attributeService.SaveAttribute(user, SystemUserAttributeNames.LastName, encryptionService.AESEncrypt(request.LastName, user));

            if ( !string.IsNullOrEmpty(request.Mobile))
                attributeService.SaveAttribute(user, SystemUserAttributeNames.Mobile, encryptionService.AESEncrypt( request.Mobile,user));

            // create activation token
            attributeService.SaveAttribute(user, SystemUserAttributeNames.AccountActivationToken, Guid.NewGuid().ToString());

            // set the data result
            result.Data = user;
            return result;
        }
Exemple #4
0
        public ActionResult Create(UserModel model)
        {
            if (ModelState.IsValid)
            {
                var request = new UserRegistrationRequest(
                    model.Username, encryptionService.GetSHAHash(StateKeyManager.TemporaryPassword, true), PublishingStatus.PendingApproval);

                // set the other properties
                request.FirstName = model.FirstName;
                request.LastName = model.LastName;
                request.Mobile = model.Mobile;
                request.IsAdministrator = model.IsAdministrator;

                // register this user
                var result = registrationService.RegisterUser(request);
                if (result.Success)
                {
                    // add the associated branches to this user
                    var associatedOffices = model.OfficeId.ToList<Guid>();
                    attributeService.SaveAttribute<List<Guid>>(result.Data, SystemAttributeNames.AssociatedBrancOffices,
                        associatedOffices);

                    result.Data.AuditHistory.Add
                    (
                        userActivityService.InsertActivity(SystemActivityLogTypeNames.EntityOfficeAssociation,
                            PrepareOfficeAssociationModel(associatedOffices,officeService,cacheManager), StateKeyManager.USER_ACTIVITY_COMMENT, request.Username)
                    );
                    userService.Update(result.Data);

                    // send the user activation email
                    messageService.SendUserEmailValidationMessage(result.Data, workContext.WorkingLanguage.RowId);

                    // return notification message
                    SuccessNotification(localizationService.GetResource("Users.Added"));
                    return RedirectToAction(SystemRouteNames.Index);
                }
            }

            //If we got this far, something failed, redisplay form
            model.AvailableOffices = base.PrepareSelectList(officeService, cacheManager);
            return View(model);
        }