/// <summary>
        /// Inserts new user
        /// </summary>
        /// <param name="user"></param>
        /// <param name="inviterName"></param>
        /// <param name="companyName"></param>
        /// <param name="allowUpdate"></param>
        /// <returns></returns>
        public async Task <bool> Add(User user, string inviterName, string companyName, bool allowUpdate)
        {
            if (user is null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            if (string.IsNullOrEmpty(inviterName))
            {
                throw new ArgumentNullException(nameof(inviterName));
            }

            var existing = await GetUser(user.Email, null);

            if (existing?.Email == user.Email)
            {
                // update roles

                existing.Roles = user.Roles;
                return(await SetUser(existing, false));
            }
            else
            {
                (string pass, string hash, string cohash) = GeneratePassword();
                user.PswHash = hash;
                user.CoHash  = cohash;
                var ret = await SetUser(user, true);

                await emailSender.SendEmail(
                    localizer[Repository_RedisRepository_UserRepository.Invitation_to_covid_testing_place],
                    user.Email,
                    user.Name,
                    new Model.Email.InvitationEmail(CultureInfo.CurrentCulture.Name, configuration["FrontedURL"], configuration["EmailSupport"], configuration["PhoneSupport"])
                {
                    Name        = user.Name,
                    Password    = pass,
                    Roles       = user.Roles?.ToArray(),
                    InviterName = inviterName,
                    CompanyName = companyName,
                    WebPath     = configuration["FrontedURL"]
                });

                if (!string.IsNullOrEmpty(user.Phone))
                {
                    await smsSender.SendSMS(user.Phone, new Message(string.Format(localizer[Repository_RedisRepository_UserRepository.Dear__0___we_have_registered_you_into_mass_covid_testing_system__Please_check_your_email_].Value, user.Name)));
                }
                return(ret);
            }
        }
Esempio n. 2
0
        public async Task <ActionResult <bool> > SendSMS([FromQuery] string phone)
        {
            try
            {
                if (!await User.IsPlaceProviderAdmin(userRepository, placeProviderRepository))
                {
                    throw new Exception("Only administrator can update visitor directly");
                }

                logger.LogInformation($"SendSMS: {User.GetEmail()} is sending test sms to {phone}");

                return(Ok(await smsSender.SendSMS(phone, new Message($"Test sms: {DateTimeOffset.Now.ToString("f")}"))));
            }
            catch (Exception exc)
            {
                logger.LogError(exc, exc.Message);

                return(BadRequest(new ProblemDetails()
                {
                    Detail = exc.Message
                }));
            }
        }