Example #1
0
        public async Task <ActionResult <IdentityResult> > RegisterOrganization([FromBody] OrganizationRegistration orgRegistration)
        {
            IdentityResult result;

            TryValidateModel(orgRegistration);

            if (ModelState.IsValid)
            {
                var organization = orgRegistration.ToOrganization();
                organization = await _memberService.CreateOrganizationAsync(organization);

                var contact = orgRegistration.ToContact();
                contact.OrganizationId = organization.Id;

                var user = orgRegistration.ToUser();
                user.Contact = contact;
                user.StoreId = WorkContext.CurrentStore.Id;
                user.Roles   = new[] { SecurityConstants.Roles.OrganizationMaintainer };

                result = await _userManager.CreateAsync(user, orgRegistration.Password);

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

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

                    await _signInManager.SignInAsync(user, isPersistent : true);

                    await _publisher.Publish(new UserLoginEvent(WorkContext, user));
                }
            }
            else
            {
                result = IdentityResult.Failed(ModelState.Values.SelectMany(x => x.Errors).Select(x => new IdentityError {
                    Description = x.ErrorMessage
                }).ToArray());
            }

            return(result);
        }
        public async Task <ActionResult <UserActionIdentityResult> > RegisterOrganization([FromBody] OrganizationRegistration orgRegistration)
        {
            UserActionIdentityResult result;

            TryValidateModel(orgRegistration);

            if (ModelState.IsValid)
            {
                var user = await _userManager.FindByEmailAsync(orgRegistration.Email);

                if (user != null)
                {
                    var error = new IdentityError
                    {
                        Description = $"Email '{orgRegistration.Email}' is already taken."
                    };

                    return(UserActionIdentityResult.Failed(error));
                }

                user = await _userManager.FindByNameAsync(orgRegistration.UserName);

                if (user != null)
                {
                    var error = new IdentityError
                    {
                        Description = $"User name '{orgRegistration.UserName}' is already taken."
                    };

                    return(UserActionIdentityResult.Failed(error));
                }

                var contact = orgRegistration.ToContact();

                if (!string.IsNullOrEmpty(orgRegistration.OrganizationName))
                {
                    var organization = orgRegistration.ToOrganization();
                    organization = await _memberService.CreateOrganizationAsync(organization);

                    contact.OrganizationId = organization.Id;
                }

                user         = orgRegistration.ToUser();
                user.Contact = contact;
                user.StoreId = WorkContext.CurrentStore.Id;
                user.Roles   = new[]
                {
                    SecurityConstants.Roles.OrganizationMaintainer
                };

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

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

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

                    await _signInManager.SignInAsync(user, isPersistent : true);

                    await _publisher.Publish(new UserLoginEvent(WorkContext, user));

                    result.MemberId = user.Id;
                }
            }
            else
            {
                result = UserActionIdentityResult
                         .Failed(ModelState.Values.SelectMany(x => x.Errors)
                                 .Select(x => new IdentityError {
                    Description = x.ErrorMessage
                })
                                 .ToArray());
            }

            return(result);
        }