Example #1
0
        public static User ToUser(this OrganizationUserRegistration registerForm)
        {
            var result = ((UserRegistration)registerForm).ToUser();

            if (!string.IsNullOrEmpty(registerForm.Role))
            {
                result.Roles = new[] { new Role {
                                           Id = registerForm.Role, Name = registerForm.Role
                                       } };
            }
            return(result);
        }
        public async Task <ActionResult <UserActionIdentityResult> > RegisterUser([FromBody] OrganizationUserRegistration registration)
        {
            UserActionIdentityResult result;

            TryValidateModel(registration);

            if (ModelState.IsValid)
            {
                //Allow to register new users only within own organization
                var authorizationResult = await _authorizationService.AuthorizeAsync(User, new Organization { Id = registration.OrganizationId }, CanEditOrganizationResourceAuthorizeRequirement.PolicyName);

                if (!authorizationResult.Succeeded)
                {
                    return(Unauthorized());
                }

                var contact = registration.ToContact();
                contact.OrganizationId = registration.OrganizationId;

                var user = registration.User;
                user.Contact = contact;
                user.StoreId = WorkContext.CurrentStore.Id;

                var creatingResult = await _userManager.CreateAsync(user, registration.Password);

                result = UserActionIdentityResult.Instance(creatingResult);
                if (result.Succeeded)
                {
                    user = await _userManager.FindByNameAsync(user.UserName);

                    await _publisher.Publish(new UserRegisteredEvent(WorkContext, user, registration));

                    result.MemberId = user.Id;
                }
            }
            else
            {
                result = UserActionIdentityResult
                         .Failed(ModelState.Values.SelectMany(x => x.Errors)
                                 .Select(x => new IdentityError {
                    Description = x.ErrorMessage
                })
                                 .ToArray());
            }
            return(result);
        }
        public static User ToUser(this OrganizationUserRegistration registerForm)
        {
            var result = ((UserRegistration)registerForm).ToUser();

            if (!string.IsNullOrEmpty(registerForm.Role))
            {
                var role = SecurityConstants.Roles.AllRoles.FirstOrDefault(x => x.Id.EqualsInvariant(registerForm.Role));
                if (role != null)
                {
                    result.Roles = new[] { role };
                }
                else
                {
                    result.Roles = new[] { new Role {
                                               Id = registerForm.Role, Name = registerForm.Role
                                           } };
                }
            }
            return(result);
        }