Пример #1
0
        public static UserDto UserToDto(ApplicationUser user)
        {
            var userDto = new UserDto();
            userDto.InjectFrom(user);

            userDto.Roles = new List<string>();
            foreach (var role in user.Roles)
            {
                userDto.Roles.Add(RoleManager.GetRoleName(role.RoleId));
            }

            userDto.UserId = user.Id;

            userDto.PrescriptionId = new List<string>();
            if (user.Prescriptions != null)
            {
                foreach (var prescription in user.Prescriptions)
                {
                    userDto.PrescriptionId.Add(prescription.PrescriptionId.ToString());
                }
            }

            userDto.SupporterCareTeamNames = new List<string>();
            if (user.SupporterCareTeams != null)
            {
                foreach(var careTeam in user.SupporterCareTeams)
                {
                    userDto.SupporterCareTeamNames.Add(careTeam.Name);
                }
            }
            return userDto;
        }
Пример #2
0
 public static ApplicationUser CreateRandomPerson()
 {
     // We need to setup an actual user who can login
     var u = new ApplicationUser();
     u.Id = Guid.NewGuid().ToString();
     u.FirstName = GetRandom(FirstNames);
     u.LastName = GetRandom(LastNames);
     u.UserName = u.FirstName + u.LastName + GetRandom(100000);
     u.Email = u.UserName + "@cloudmedic.io";
     u.PhoneNumber = String.Format("({0}) {1}-{2}",
         GetRandom(999),
         GetRandom(999),
         GetRandom(9999));
     u.EmailConfirmed = true;
     u.PhoneNumberConfirmed = true;
     u.DateOfBirth = GetRandomDob();
     u.GenderEnum = (GenderEnum)GetRandom(1, 3);
     u.Specialty = string.Empty;
     u.SecurityStamp = Guid.NewGuid().ToString("D");
     return u;
 }
Пример #3
0
        public async Task<IHttpActionResult> RegisterExternal(RegisterExternalBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var info = await Authentication.GetExternalLoginInfoAsync();
            if (info == null)
            {
                return InternalServerError();
            }

            var user = new ApplicationUser() { UserName = model.Email, Email = model.Email };

            IdentityResult result = await UserManager.CreateAsync(user);
            if (!result.Succeeded)
            {
                return GetErrorResult(result);
            }

            result = await UserManager.AddLoginAsync(user.Id, info.Login);
            if (!result.Succeeded)
            {
                return GetErrorResult(result); 
            }
            return Ok();
        }
Пример #4
0
        public async Task<IHttpActionResult> SupporterRegistration(RegisterSupporterBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var supporter = new ApplicationUser()
            {
                UserName = model.UserName,
                Email = model.Email,
                FirstName = model.FirstName,
                LastName = model.LastName,
                Gender = model.Gender,
                DOB = model.DOB,
                PhoneNumber = model.PhoneNumber,
                Specialty = ""
            };

            supporter.Roles.Add(new IdentityUserRole()
            {
                RoleId = RoleManager.GetRoleId(RoleId.Supporter),
                UserId = supporter.Id
            });

            IdentityResult result = await UserManager.CreateAsync(supporter, model.Password);

            if (!result.Succeeded)
            {
                return GetErrorResult(result);
            }

            return Ok();
        }
Пример #5
0
 private bool PrescriptionAssigned(Prescription p, ApplicationUser user)
 {
     return user.Prescriptions.Contains(p);
 }
Пример #6
0
 public static bool IsPatient(ApplicationUser user)
 {
     foreach (var role in user.Roles)
     {
         if (RoleManager.IsRole(role, RoleId.Patient))
             return true;
     }
     return false;
 }
Пример #7
0
        public async Task<IHttpActionResult> PostUser(CreateUserBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var password = Randomizer.GetRandomString(10);

            var user = new ApplicationUser()
            {
                UserName = model.FirstName + model.LastName + Randomizer.GetRandom(100000),
                Email = model.Email,
                FirstName = model.FirstName,
                LastName = model.LastName,
                Gender = model.Gender,
                DOB = model.DOB,
                PhoneNumber = model.PhoneNumber,
                Specialty = model.Specialty
            };
            
            var identityResult = await _userManager.CreateAsync(user, password);

            if (!identityResult.Succeeded)
                return BuildErrorResult(identityResult);
            
            Crypteron.CipherDb.Session.Unseal(user, _db);
            identityResult = await _userManager.AddToRolesAsync(user.Id, model.Roles.ToArray());


            Crypteron.CipherDb.Session.Unseal(user, _db);

            if (!identityResult.Succeeded)
                return BuildErrorResult(identityResult);

            // Send an invitation to login and change password
            MailSender sender = new MailSender();
            string mailBody = "Dear " + user.FirstName + " " + user.LastName + ", you have been added to CloudMedic by an administrator.\n\nPlease login with your assigned username and password:\n\nUsername: "******"\nPassword: "******"\n\n After logging in, change your password under the profile tab.";

            sender.SendInvite(mailBody, user.Email);

            return Created("users/" + user.Id, ToDto.UserToDto(user));       
        }
Пример #8
0
        public async Task<IHttpActionResult> PutUser(string id, ApplicationUser user)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (string.IsNullOrWhiteSpace(id) || !id.Equals(user.Id))
            {
                return BadRequest();
            }

            var identityResult = await _userManager.UpdateAsync(user);
            if (!identityResult.Succeeded)
                return BuildErrorResult(identityResult);

            return StatusCode(HttpStatusCode.NoContent);
        }