public ActionResult ChangeProfile(UserViewModel model)
        {
            try
            {
                var userId = model.Id;

                var context = new iHospitalDataContext();

                var user = context.AspNetUsers.FirstOrDefault(u => u.Id == userId);

                user.FirstName   = model.FName;
                user.LastName    = model.LName;
                user.PhoneNumber = model.Phone;

                context.SubmitChanges();

                return(Json(new { success = true }));
                //return RedirectToAction("Index");
            }
            catch (Exception e)
            {
                return(Json(new
                {
                    success = false,
                    error = e.Message
                }));
            }
        }
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email, LockoutEnabled = false
                };
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                    //Assign Patient role to user. Only Patient can be register. Other roles should be created inside portal
                    await this.UserManager.AddToRoleAsync(user.Id, "Patient");

                    //Create Patient record
                    var p = new Patient()
                    {
                        Id    = user.Id,
                        Email = user.Email,

                        DateCreated  = DateTime.Now,
                        DateModified = DateTime.Now,
                    };
                    var dbcontext = new iHospitalDataContext();
                    dbcontext.Patients.InsertOnSubmit(p);
                    dbcontext.SubmitChanges();


                    //Redirect to home page of Patient Portal after created user successfully
                    return(RedirectToAction("Index", "PatientPortal"));

                    // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771
                    // Send an email with this link
                    // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

                    //return RedirectToAction("Index", "Home");
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
        public async System.Threading.Tasks.Task <ActionResult> AddStaffAsync(UserViewModel model)
        {
            try
            {
                var userApp = new ApplicationUser {
                    UserName = model.Email, Email = model.Email, LockoutEnabled = false
                };
                var result = await UserManager.CreateAsync(userApp, model.NewPwd);

                if (result.Succeeded)
                {
                    await UserManager.AddToRoleAsync(userApp.Id, model.Role);

                    var context = new iHospitalDataContext();

                    var user = context.AspNetUsers.FirstOrDefault(u => u.Id == userApp.Id);
                    user.FirstName   = model.FName;
                    user.LastName    = model.LName;
                    user.PhoneNumber = model.Phone;

                    var s = new Staff()
                    {
                        Id           = user.Id,
                        FirstName    = user.FirstName,
                        LastName     = user.LastName,
                        Phone        = user.PhoneNumber,
                        DepartmentId = "test",
                        Email        = user.Email,
                        DateModified = DateTime.Now,
                        DateCreated  = DateTime.Now
                    };
                    context.Staffs.InsertOnSubmit(s);
                    context.SubmitChanges();

                    return(Json(new { success = true }));
                }

                return(Json(new { success = false, error = result.Errors.FirstOrDefault() }));
            }
            catch (Exception e)
            {
                return(Json(new { success = false, error = e.Message }));
            }
        }
        public ActionResult SetAppointment(string doctor, string date, string time, string problem)
        {
            try
            {
                var userId = User.Identity.GetUserId();

                var datetime = Convert.ToDateTime(date + " " + time);

                var apm = new Appointment()
                {
                    Id             = Guid.NewGuid().ToString(),
                    PatientId      = userId,
                    DoctorId       = doctor,
                    SpecialtyId    = "test",
                    TimeStart      = datetime,
                    TimeEnd        = datetime.AddMinutes(30),
                    MedicalProblem = problem,
                    DateCreated    = DateTime.Now,
                    DateModified   = DateTime.Now,
                };

                var context = new iHospitalDataContext();
                context.Appointments.InsertOnSubmit(apm);
                context.SubmitChanges();

                return(Json(new { success = true }));
                //return RedirectToAction("Index");
            }
            catch (Exception e)
            {
                return(Json(new
                {
                    success = false,
                    error = e.Message
                }));
            }
        }
示例#5
0
        //Create default User roles and Admin user for login
        private void CreateRolesandUsers()
        {
            ApplicationDbContext context = new ApplicationDbContext();

            var roleManager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(context));
            var userManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(context));

            //Creat Admin role and 1st Admin user
            if (!roleManager.RoleExists("Admin"))
            {
                //Create Admin role
                var roleAdmin = new IdentityRole
                {
                    Id   = "0ded18bc-cead-489a-b107-0fadb1cdf962",
                    Name = "Admin"
                };
                roleManager.Create(roleAdmin);

                //Define 1st Admin super user who will maintain the website
                var user = new ApplicationUser
                {
                    UserName = "******",
                    Email    = "*****@*****.**"
                };
                var userPWD = "Pwd!23";

                //Create 1st Admin user
                var chkUser = userManager.Create(user, userPWD);

                //Add user to role Admin
                if (chkUser.Succeeded)
                {
                    var result = userManager.AddToRole(user.Id, "Admin");
                }
            }

            //Create AdministrativeStaff role
            if (!roleManager.RoleExists("AdministrativeStaff"))
            {
                var role = new IdentityRole
                {
                    Id   = "dd9bd5de-674d-4f55-b526-d945d9456da7",
                    Name = "AdministrativeStaff"
                };
                roleManager.Create(role);
            }

            //Create MedicalStaff role
            if (!roleManager.RoleExists("MedicalStaff"))
            {
                var role = new IdentityRole
                {
                    Id   = "90f71088-0b87-43d1-8b69-d3fce07cac72",
                    Name = "MedicalStaff"
                };
                roleManager.Create(role);
            }

            //Create Patient role
            if (!roleManager.RoleExists("Patient"))
            {
                var role = new IdentityRole
                {
                    Id   = "3929b37d-88ba-45db-b727-cba789fd2833",
                    Name = "Patient"
                };
                roleManager.Create(role);
            }

            var dbcontext = new iHospitalDataContext();

            if (dbcontext.Departments.FirstOrDefault(d => d.Id == "test") == null)
            {
                var d = new Department()
                {
                    Id           = "test",
                    DateModified = DateTime.Now,
                    DateCreated  = DateTime.Now,
                    Name         = "test"
                };
                dbcontext.Departments.InsertOnSubmit(d);
                dbcontext.SubmitChanges();
            }
        }