示例#1
0
        public IActionResult DeleteInvitationRequest(int invitationRequestId)
        {
            var request = _inviteRequestService.Get(invitationRequestId);

            if (request == null)
            {
                return(NotFound());
            }
            _inviteRequestService.Delete(request);
            return(R.Success.Result);
        }
        public IActionResult Register(RegisterModel registerModel)
        {
            //are registrations enabled?
            if (_userSettings.UserRegistrationDefaultMode == RegistrationMode.Disabled)
            {
                return(R.Fail.With("error", T("New registrations are disabled at the moment")).Result);
            }
            var      inviteCode = registerModel.InviteCode;
            UserCode userCode   = null;

            if (_userSettings.UserRegistrationDefaultMode == RegistrationMode.InviteOnly)
            {
                if (inviteCode.IsNullEmptyOrWhiteSpace())
                {
                    return(R.Fail.With("error", T("Registrations are allowed only by invitation")).Result);
                }
                userCode = _userCodeService.GetUserCode(inviteCode, UserCodeType.RegistrationInvitation);
                if (userCode.Email != registerModel.Email || !IsCodeValid(userCode))
                {
                    return(R.Fail.With("error", T("Registrations are allowed only by invitation")).Result);
                }
            }
            //validate consents first
            //get one time consents
            var consents = _consentService.Get(x => x.OneTimeSelection && x.Published).ToList();

            if (consents.Any(x => x.IsRequired))
            {
                foreach (var requiredConsent in consents.Where(x => x.IsRequired))
                {
                    var sentModel = registerModel.Consents.FirstOrDefault(x => x.Id == requiredConsent.Id);
                    if (sentModel == null || sentModel.ConsentStatus != ConsentStatus.Accepted)
                    {
                        return(R.Fail.With("error", T("Please consent to '" + requiredConsent.Title + "'")).Result);
                    }
                }
            }
            var user = new User()
            {
                Email           = registerModel.Email,
                Password        = registerModel.Password,
                CreatedOn       = DateTime.UtcNow,
                UpdatedOn       = DateTime.UtcNow,
                IsSystemAccount = false,
                Guid            = Guid.NewGuid(),
                Active          = _userSettings.UserRegistrationDefaultMode == RegistrationMode.Immediate || _userSettings.UserRegistrationDefaultMode == RegistrationMode.InviteOnly
            };

            if (user.Active)
            {
                user.FirstActivationDate = DateTime.UtcNow;
            }
            //register this user
            var registrationStatus = _userRegistrationService.Register(user, _securitySettings.DefaultPasswordStorageFormat);

            if (registrationStatus == UserRegistrationStatus.FailedAsEmailAlreadyExists)
            {
                return(R.Fail.With("message", "A user with this email is already registered").Result);
            }

            var roleId = _roleService.Get(x => x.SystemName == SystemRoleNames.Registered).First().Id;

            //assign role to the user
            _roleService.SetUserRoles(user.Id, new[] { roleId });

            if (registerModel.Consents != null && registerModel.Consents.Any())
            {
                //save the consents
                var consentDictionary = registerModel.Consents.ToDictionary(x => x.Id, x => x.ConsentStatus);
                _gdprService.SetUserConsents(user.Id, consentDictionary);
            }

            //delete the invite code & user code if any
            _inviteRequestService.Delete(x => x.Email == registerModel.Email);
            if (userCode != null)
            {
                _userCodeService.Delete(userCode);
            }
            var verificationLink = "";

            if (registerModel.InviteCode.IsNullEmptyOrWhiteSpace())
            {
                //if there was no invite code, the email needs to be verified (if the admin wants so)
                if (_userSettings.UserRegistrationDefaultMode == RegistrationMode.WithActivationEmail)
                {
                    userCode = _userCodeService.GetUserCode(user.Id, _userSettings.UseNumericCodeForActivationEmail ? UserCodeType.EmailOtp : UserCodeType.EmailVerification);
                    var verificationCode = userCode.Code;
                    verificationLink = verificationCode;
                    if (!_userSettings.UseNumericCodeForActivationEmail)
                    {
                        verificationLink =
                            ApplicationEngine.RouteUrl(RouteNames.VerifyEmail, new { code = verificationCode }, true);
                    }
                }
            }

            //do we have any affiliate?
            var affiliate = ApplicationEngine.CurrentAffiliate;

            if (affiliate != null)
            {
                user.ReferrerId = affiliate.Id;
                _userService.Update(user);
            }

            //raise the event
            RaiseEvent(NamedEvent.UserRegistered, user, verificationLink);
            if (user.Active)
            {
                RaiseEvent(NamedEvent.UserActivated, user);
            }
            return(R.Success.With("mode", _userSettings.UserRegistrationDefaultMode).With("numericActivation", _userSettings.UseNumericCodeForActivationEmail).Result);
        }