Ejemplo n.º 1
0
        public async Task <IHttpActionResult> CreateAdmin(CreateAdminDto createAdminDto)
        {
            _logger.Debug(string.Format("Begin. Email: [{0}], FirstName: [{1}], Gender: [{2}], LastName: [{3}], PhoneNumber: [{4}], Title: [{5}], UserName: [{6}]",
                                        createAdminDto.Email,
                                        createAdminDto.FirstName,
                                        createAdminDto.Gender.ToString(),
                                        createAdminDto.LastName,
                                        createAdminDto.PhoneNumber,
                                        createAdminDto.Title.ToString(),
                                        createAdminDto.UserName));
            if (!ModelState.IsValid)
            {
                _logger.Error(string.Format(
                                  "Model state is not valid. ModelState: [{0}]",
                                  string.Join(Environment.NewLine, ModelState.Select(x => string.Format("{0}: {1}", x.Key, x.Value)))));
                return(BadRequest(ModelState));
            }

            var user = new NdUser()
            {
                Email                = createAdminDto.Email,
                EmailConfirmed       = true,
                FirstName            = createAdminDto.FirstName,
                Gender               = createAdminDto.Gender.Value,
                LastName             = createAdminDto.LastName,
                PhoneNumber          = createAdminDto.PhoneNumber,
                PhoneNumberConfirmed = true,
                Title                = createAdminDto.Title.Value,
                UserName             = createAdminDto.UserName
            };

            IdentityResult addUserResult = await NdUserManager.CreateAsync(user, createAdminDto.Password);

            if (!addUserResult.Succeeded)
            {
                _logger.Error(string.Format(
                                  "Create admin failed. Email: [{0}], Reason: [{1}]",
                                  createAdminDto.Email,
                                  string.Join(Environment.NewLine, addUserResult.Errors)));
                return(GetErrorResult(addUserResult));
            }

            IdentityResult addUserToRoleResult = await NdUserManager.AddToRoleAsync(user.Id, "Admin");

            if (!addUserToRoleResult.Succeeded)
            {
                _logger.Error(string.Format(
                                  "Add admin to roles failed. Email: [{0}], Reason: [{1}]",
                                  createAdminDto.Email,
                                  string.Join(Environment.NewLine, addUserResult.Errors)));
                return(GetErrorResult(addUserResult));
            }

            Uri locationHeader = new Uri(Url.Link("GetUserById", new { id = user.Id }));

            _logger.Debug(string.Format("Admin created successfully. Email: [{0}]", createAdminDto.Email));
            return(Created(locationHeader, _factory.Create(user)));
        }
Ejemplo n.º 2
0
        public async Task <IHttpActionResult> ManageUsersInRole(UsersInRoleDto usersInRoleDto)
        {
            var role = await NdRoleManager.FindByIdAsync(usersInRoleDto.Id);

            if (role == null)
            {
                ModelState.AddModelError("", "Role does not exist");
                return(BadRequest(ModelState));
            }

            foreach (string user in usersInRoleDto.EnrolledUsers)
            {
                var ndUser = await NdUserManager.FindByIdAsync(user);

                if (ndUser == null)
                {
                    ModelState.AddModelError("", string.Format("User: {0} does not exists", user));
                    continue;
                }

                if (!NdUserManager.IsInRole(user, role.Name))
                {
                    IdentityResult result = await NdUserManager.AddToRoleAsync(user, role.Name);

                    if (!result.Succeeded)
                    {
                        ModelState.AddModelError("", string.Format("User: {0} could not be added to role", user));
                    }
                }
            }

            foreach (string user in usersInRoleDto.RemovedUsers)
            {
                var ndUser = await NdUserManager.FindByIdAsync(user);

                if (ndUser == null)
                {
                    ModelState.AddModelError("", string.Format("User: {0} does not exists", user));
                    continue;
                }

                IdentityResult result = await NdUserManager.RemoveFromRoleAsync(user, role.Name);

                if (!result.Succeeded)
                {
                    ModelState.AddModelError("", string.Format("User: {0} could not be removed from role", user));
                }
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            return(Ok());
        }
Ejemplo n.º 3
0
        public async Task <IHttpActionResult> CreateTherapist(CreateTherapistDto createTherapistDto)
        {
            _logger.Debug(string.Format("Begin. Clinic: [{0}], Email: [{1}], FirstName: [{2}], Gender: [{3}], LastName: [{4}], PhoneNumber: [{5}], Title: [{6}]",
                                        createTherapistDto.Clinic,
                                        createTherapistDto.Email,
                                        createTherapistDto.FirstName,
                                        createTherapistDto.Gender.ToString(),
                                        createTherapistDto.LastName,
                                        createTherapistDto.PhoneNumber,
                                        createTherapistDto.Title.ToString()));
            if (!ModelState.IsValid)
            {
                _logger.Error(string.Format(
                                  "Model state is not valid. ModelState: [{0}]",
                                  string.Join(Environment.NewLine, ModelState.Select(x => string.Format("{0}: {1}", x.Key, x.Value)))));
                return(BadRequest(ModelState));
            }

            var user = new NdUser()
            {
                Clinic      = createTherapistDto.Clinic,
                Email       = createTherapistDto.Email,
                FirstName   = createTherapistDto.FirstName,
                Gender      = createTherapistDto.Gender,
                LastName    = createTherapistDto.LastName,
                PhoneNumber = createTherapistDto.PhoneNumber,
                Title       = createTherapistDto.Title,
                UserName    = createTherapistDto.Email
            };

            var            password      = PasswordGenerator.Generate();
            IdentityResult addUserResult = await NdUserManager.CreateAsync(user, password);

            if (!addUserResult.Succeeded)
            {
                _logger.Error(string.Format(
                                  "Create user failed. Email: [{0}], Reason: [{1}]",
                                  createTherapistDto.Email,
                                  string.Join(Environment.NewLine, addUserResult.Errors)));
                return(GetErrorResult(addUserResult));
            }

            IdentityResult addUserToRoleResult = await NdUserManager.AddToRoleAsync(user.Id, "Therapist");

            if (!addUserToRoleResult.Succeeded)
            {
                _logger.Error(string.Format(
                                  "Add user to roles failed. Email: [{0}], Reason: [{1}]",
                                  createTherapistDto.Email,
                                  string.Join(Environment.NewLine, addUserResult.Errors)));
                return(GetErrorResult(addUserResult));
            }

            try
            {
                Directory.CreateDirectory(HttpContext.Current.Server.MapPath(Path.Combine("~/Results", user.Id)));
            }
            catch (Exception ex)
            {
                _logger.Error(string.Format("Error creating folder for therapist. Email: [{0}]", createTherapistDto.Email), ex);
                return(InternalServerError(ex));
            }

            try
            {
                string code = await NdUserManager.GenerateEmailConfirmationTokenAsync(user.Id);

                var callbackUrl = new Uri(Url.Link("ConfirmEmailRoute", new { userId = user.Id, code = code }));
                await NdUserManager.SendEmailAsync(user.Id, "Confirm your account", NdEmailService.CreateConfirmEmailWithPasswordBody(callbackUrl.ToString(), password));
            }
            catch (Exception ex)
            {
                _logger.Error(string.Format("Error sending ConfirmEmail email for therapist. Email: [{0}]", createTherapistDto.Email), ex);
                return(InternalServerError(ex));
            }

            Uri locationHeader = new Uri(Url.Link("GetUserById", new { id = user.Id }));

            return(Created(locationHeader, _factory.Create(user)));
        }