Exemplo n.º 1
0
 public ActionResult Create()
 {
     if (User.IsInRole("Administrator"))
     {
         MembershipUserCollection users = Membership.GetAllUsers();
         var model = new CreateStudentViewModel
         {
             Users = users.OfType <MembershipUser>().Select(x => new SelectListItem
             {
                 Value = x.UserName,
                 Text  = x.UserName,
             })
         };
         return(View(model));
     }
     if (User.IsInRole("Default"))
     {
         MembershipUserCollection users = Membership.FindUsersByName(User.Identity.Name);
         var model = new CreateStudentViewModel
         {
             Users = users.OfType <MembershipUser>().Select(x => new SelectListItem
             {
                 Value = x.UserName,
                 Text  = x.UserName,
             })
         };
         return(View(model));
     }
     return(View(/*user has a role that is not "Default" or "Administrator"  Needs error message*/));
 }
Exemplo n.º 2
0
        public ActionResult Create(CreateStudentViewModel studentModel, string selectedUser, string lastName,
                                   string firstMidName, string email)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var student = new Student();

                    //Establish the student data
                    student.UserName       = selectedUser;
                    student.LastName       = lastName;
                    student.FirstMidName   = firstMidName;
                    student.Email          = email;
                    student.EnrollmentDate = DateTime.Now;

                    db.Students.Add(student);                                   //inputs student data into database (is not saved yet)
                    db.SaveChanges();                                           //saves the student to database

                    MembershipUser user = Membership.GetUser(student.UserName); //gets the actual user
                    Roles.AddUserToRole(user.UserName, "Student");              //takes the user and sets role to student

                    // assigns Student data to the profile of the user (so the user is associated with this specified Student data)
                    CustomProfile profile = CustomProfile.GetUserProfile(student.UserName);
                    profile.FilledStudentInfo = "yes";
                    profile.Save();

                    return(RedirectToAction("Index"));
                }
            }
            catch (DataException)
            {
                //Log the error (add a variable name after DataException)
                ModelState.AddModelError("",
                                         "Saving failed for some reason.  You may have left some information blank.  Please try again (several times in several different ways if possible (i.e. try using a different computer) - if the problem persists see your system administrator.");
            }

            // This code block is here to allow the page to render in case we get a DataException and have to re-display the screen.
            MembershipUserCollection users = Membership.GetAllUsers();
            var model = new CreateStudentViewModel
            {
                Users = users.OfType <MembershipUser>().Select(x => new SelectListItem
                {
                    Value = x.UserName,
                    Text  = x.UserName,
                })
            };

            return(View(model));
        }