Exemple #1
0
        public async Task <UserShortDataModel> AdminCreate(UserCreateModel model)
        {
            if (model == null)
            {
                throw new ModelDamagedException(nameof(model), "is required");
            }

            if ((await _appUserManager.FindByEmailAsync(model.Email)) != null)
            {
                throw new EntityExistsException(nameof(model.Email), model.Email, typeof(AppUser));
            }

            var user = new AppUser
            {
                Birthday  = new DateTime(),
                FirstName = model.FirstName,
                LastName  = model.LastName,
                Email     = model.Email,
                Tels      = model.Tels?.Select((x) => new MRUserTel
                {
                    Name = x.Name,
                    Tel  = x.Number
                }).ToList() ?? new List <MRUserTel>(),
                UserName = model.Email
            };

            var createResult = await _appUserManager.CreateAsync(user);

            if (!createResult.Succeeded)
            {
                throw new SystemException("Can not create user");
            }

            await _appUserRepository.AddToRoleAsync(user, "USER", new System.Threading.CancellationToken());

            UserCreateTemplateModel templateModel = new UserCreateTemplateModel
            {
                Provider    = "MR Identity",
                CallbackUrl = "https://google.com",
                Email       = user.Email,
                FirstName   = user.FirstName,
                LastName    = user.LastName
            };

            var emailBody = await _templateParser.Render(null, EmailTemplateCollection.USER_INVITE, templateModel);

            await _emailSendTaskRepository.InsertEmail(user.Email, "MR identity invite", emailBody, Infrastructure.Entities.Tasks.EmailTaskBot.MadRatBot);

            return(_mapper.Map <UserShortDataModel>(user));
        }
        /// <summary>
        /// Invite new user or connect exists
        /// </summary>
        /// <param name="model">Provider create user model</param>
        /// <returns>UserDisplayModel</returns>
        public async Task <UserDisplayModel> InviteUser(ProviderUserCreateModel model)
        {
            var currentUser = await GetCurrentUser();

            if (!await _providerRepository.ExistsWithOwner(model.ProviderId, currentUser.Id))
            {
                throw new AccessDeniedException(currentUser.Id, typeof(AppUser));
            }

            if (model.Roles == null || !model.Roles.Any())
            {
                throw new AccessDeniedException("", typeof(AppUser));
            }

            model.Roles = model.Roles.Select(x => x.ToUpper()).ToList();

            var pShort = await _providerRepository.GetShortById(model.ProviderId);

            var roles = await _providerRepository.GetRolesById(model.ProviderId);

            var rolesToUse = roles.Where(x => model.Roles.Contains(x.Name)).ToList();

            var existsUser = await _appUserRepository.FindByEmailAsync(model.Email.ToLower(), new System.Threading.CancellationToken());

            if (existsUser == null)
            {
                existsUser = new AppUser
                {
                    Email     = model.Email.ToLower(),
                    FirstName = model.FirstName,
                    LastName  = model.LastName,
                    Tels      = new List <MRUserTel>(),
                    Status    = UserStatus.Invited,
                    UserName  = model.Email.ToLower(),
                };

                existsUser.ConnectedProviders = new List <AppUserProvider>
                {
                    new AppUserProvider
                    {
                        ProviderId   = pShort.Id,
                        ProviderName = pShort.Name,
                        Roles        = rolesToUse,
                        Metadata     = new List <AppUserProviderMeta>(),
                        UpdatedTime  = DateTime.UtcNow
                    }
                };

                var insertResult = await _appUserManager.CreateAsync(existsUser);

                if (!insertResult.Succeeded)
                {
                    throw new MRException();
                }

                await _appUserManager.AddToRoleAsync(existsUser, AppUserRoleList.USER);
            }
            else if (!existsUser.ConnectedProviders.Any(x => x.ProviderId == pShort.Id))
            {
                AppUserProvider provider = new AppUserProvider
                {
                    ProviderId   = pShort.Id,
                    Metadata     = new List <AppUserProviderMeta>(),
                    ProviderName = pShort.Name,
                    Roles        = rolesToUse,
                    UpdatedTime  = DateTime.UtcNow
                };

                if ((await _appUserRepository.AddProvider(existsUser.Id, provider)).ModifiedCount != 1)
                {
                    throw new MRException();
                }
            }
            else
            {
                throw new MRException();
            }

            Infrastructure.Model.Template.User.ProviderInviteTemplate templateModel = new Infrastructure.Model.Template.User.ProviderInviteTemplate
            {
                FirstName    = existsUser.FirstName,
                LastName     = existsUser.LastName,
                LoginLink    = "https://google.com",
                ProviderName = pShort.Name
            };

            var email = await _templateParser.Render("Email", "ProviderInvite", templateModel);

            await _emailSendTaskRepository.InsertEmail(existsUser.Email, "Mad Rat Studio invite", email, Infrastructure.Entities.Tasks.EmailTaskBot.MadRatBot);

            return(_mapper.Map <UserDisplayModel>(existsUser));
        }