public ActionResult Index(User user, HttpPostedFileBase image, FormCollection formValues)
        {
            if (!Roles.RoleExists(user.userName))
                return View("Error");
            else
            {

                    if (image != null)
                    {
                        user.ImageMimeType = image.ContentType;
                        user.ImageData = new byte[image.ContentLength];
                        image.InputStream.Read(user.ImageData, 0, image.ContentLength);
                    }
                    // save the User
                    var result = repository.SaveUser(user);
                    if (result > 0)
                    // add a message to the viewbag
                        TempData["message"] = string.Format("{0} has been saved", user.name);
                    else
                        TempData["message"] = string.Format("Unsuccessful procedure");
                    // return the user to the list
                    return RedirectToAction("Index", "Home");

            }
        }
 public int SaveUser(User user)
 {
     if (user.id == 0)
     {
         context.Users.Add(user);
     }
     else
     {
         context.Entry(user).State = System.Data.EntityState.Modified;
     }
     var result = context.SaveChanges();
     return result;
 }
 public int DeleteUser(User user)
 {
     context.Users.Remove(user);
     var result = context.SaveChanges();
     return result;
 }
        public ActionResult RegiterInSystem(User user, HttpPostedFileBase image, FormCollection formValues)
        {
            var result = 0;
            if (ModelState.IsValid)
            {
                if (user.CreatedOn == null)
                {
                    user.CreatedBy = User.Identity.Name;
                    user.CreatedOn = DateTime.Now;
                }
                else
                {
                    user.ModifiedBy = User.Identity.Name;
                    user.ModifiedOn = DateTime.Now;
                }

                if (image != null)
                {
                    user.ImageMimeType = image.ContentType;
                    user.ImageData = new byte[image.ContentLength];
                    image.InputStream.Read(user.ImageData, 0, image.ContentLength);
                }

                if (formValues["admin"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)[0].Equals("false") &&
                        formValues["instructor"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)[0].Equals("false") &&
                        formValues["student"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)[0].Equals("false") &&
                        formValues["ClassManagement"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)[0].Equals("false") &&
                        formValues["CourseManagement"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)[0].Equals("false") &&
                        formValues["GroupManagement"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)[0].Equals("false") &&
                        formValues["SystemManagement"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)[0].Equals("false") &&
                        formValues["TestManagement"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)[0].Equals("false") &&
                        formValues["UserManagement"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)[0].Equals("false"))
                    return View("Error");
                // Save the role in userInRole Table
                users[0] = user.userName;
                if (formValues["admin"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)[0].Equals("true"))
                {
                    roles[0] = "admin";
                    // save the User
                    result = repository.SaveUser(user);
                    Roles.AddUsersToRoles(users, roles);
                }
                else
                {
                    roles[0] = "admin";
                    Roles.RemoveUsersFromRoles(users, roles);
                }
                if (formValues["instructor"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)[0].Equals("true"))
                {
                    roles[0] = "instructor";
                    // save the User
                    result = repository.SaveUser(user);
                    Roles.AddUsersToRoles(users, roles);
                }
                else
                {
                    roles[0] = "instructor";
                    Roles.RemoveUsersFromRoles(users, roles);
                }
                if (formValues["student"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)[0].Equals("true"))
                {
                    roles[0] = "student";
                    // save the User
                    result = repository.SaveUser(user);
                    Roles.AddUsersToRoles(users, roles);
                }
                else
                {
                    roles[0] = "student";
                    Roles.RemoveUsersFromRoles(users, roles);
                }
                if (formValues["ClassManagement"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)[0].Equals("true"))
                {
                    roles[0] = "ClassManagement";
                    // save the User
                    result = repository.SaveUser(user);
                    Roles.AddUsersToRoles(users, roles);
                }
                else
                {
                    roles[0] = "ClassManagement";
                    Roles.RemoveUsersFromRoles(users, roles);
                }
                if (formValues["CourseManagement"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)[0].Equals("true"))
                {
                    roles[0] = "CourseManagement";
                    // save the User
                    result = repository.SaveUser(user);
                    Roles.AddUsersToRoles(users, roles);
                }
                else
                {
                    roles[0] = "CourseManagement";
                    Roles.RemoveUsersFromRoles(users, roles);
                }
                if (formValues["GroupManagement"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)[0].Equals("true"))
                {
                    roles[0] = "GroupManagement";
                    // save the User
                    result = repository.SaveUser(user);
                    Roles.AddUsersToRoles(users, roles);
                }
                else
                {
                    roles[0] = "GroupManagement";
                    Roles.RemoveUsersFromRoles(users, roles);
                }
                if (formValues["SystemManagement"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)[0].Equals("true"))
                {
                    roles[0] = "SystemManagement";
                    // save the User
                    result = repository.SaveUser(user);
                    Roles.AddUsersToRoles(users, roles);
                }
                else
                {
                    roles[0] = "SystemManagement";
                    Roles.RemoveUsersFromRoles(users, roles);
                }
                if (formValues["TestManagement"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)[0].Equals("true"))
                {
                    roles[0] = "TestManagement";
                    // save the User
                    result = repository.SaveUser(user);
                    Roles.AddUsersToRoles(users, roles);
                }
                else
                {
                    roles[0] = "TestManagement";
                    Roles.RemoveUsersFromRoles(users, roles);
                }
                if (formValues["UserManagement"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)[0].Equals("true"))
                {
                    roles[0] = "UserManagement";
                    // save the User
                    result = repository.SaveUser(user);
                    Roles.AddUsersToRoles(users, roles);
                }
                else
                {
                    roles[0] = "UserManagement";
                    Roles.RemoveUsersFromRoles(users, roles);
                }

                // add a message to the viewbag
                if (result > 0)
                {
                    TempData["message"] = string.Format("{0} has been saved", user.name);
                    var MySmtp = IsmtpRepository.Smtp.First();
                    // send notification email through gmail
                    // email address "*****@*****.**"
                    // password "P@ttersonetsemail" (Patterson employee training system email)
                    System.Net.Mail.MailMessage email = new System.Net.Mail.MailMessage();
                    email.To.Add(user.email);
                    email.Subject = "Registeration on system";
                    var fromAddress = new MailAddress(MySmtp.user);
                    email.From = fromAddress;
                    email.Body = "This is an automated message from 'Patterson Employee Training System' to inform you that you have been registered on the system and you can logon since now.";

                    System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(MySmtp.server);
                    smtp.UseDefaultCredentials = false;
                    smtp.Credentials = new System.Net.NetworkCredential(fromAddress.Address, MySmtp.password);
                    smtp.EnableSsl = true;
                    smtp.Port = MySmtp.port;

                    smtp.Send(email);
                }
                else
                    TempData["message"] = string.Format("Unsuccessful procedure!");

                // return the user to the list
                return RedirectToAction("Index");
            }
            else
            {
                // there is something wrong with the data values
                return View(user);
            }
        }
        public ActionResult Register(string user)
        {
            var curAspUser = Membership.GetAllUsers().Cast<MembershipUser>().FirstOrDefault(u => u.UserName == user);
            var roles = Roles.GetAllRoles();
            List<string> roleList = new List<string>();
            foreach (var r in roles)
            {
                roleList.Add(r);
            }
            ViewData["Roles"] = roleList;
            var userToRegister = new User();
            userToRegister.userName = curAspUser.UserName;
            userToRegister.email = curAspUser.Email;

            return View("RegiterInSystem", userToRegister);
        }
        public ActionResult Edit(User user, HttpPostedFileBase image, FormCollection formValues)
        {
            var result = 0;
            if (ModelState.IsValid)
            {
                if (user.CreatedOn == null)
                {
                    user.CreatedBy = User.Identity.Name;
                    user.CreatedOn = DateTime.Now;
                }
                else
                {
                    user.ModifiedBy = User.Identity.Name;
                    user.ModifiedOn = DateTime.Now;
                }

                if (image != null)
                {
                    user.ImageMimeType = image.ContentType;
                    user.ImageData = new byte[image.ContentLength];
                    image.InputStream.Read(user.ImageData, 0, image.ContentLength);
                }

                if (formValues["admin"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)[0].Equals("false") &&
                        formValues["instructor"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)[0].Equals("false") &&
                        formValues["student"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)[0].Equals("false") &&
                        formValues["ClassManagement"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)[0].Equals("false") &&
                        formValues["CourseManagement"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)[0].Equals("false") &&
                        formValues["GroupManagement"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)[0].Equals("false") &&
                        formValues["SystemManagement"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)[0].Equals("false") &&
                        formValues["TestManagement"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)[0].Equals("false") &&
                        formValues["UserManagement"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)[0].Equals("false"))
                    return View("Error");
                // Save the role in userInRole Table
                users[0] = user.userName;
                if (formValues["admin"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)[0].Equals("true"))
                {
                    roles[0] = "admin";
                    // save the User
                    result = repository.SaveUser(user);
                    Roles.AddUsersToRoles(users, roles);
                }
                else
                {
                    roles[0] = "admin";
                    Roles.RemoveUsersFromRoles(users, roles);
                }
                if (formValues["instructor"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)[0].Equals("true"))
                {
                    roles[0] = "instructor";
                    // save the User
                    result = repository.SaveUser(user);
                    Roles.AddUsersToRoles(users, roles);
                }
                else
                {
                    roles[0] = "instructor";
                    Roles.RemoveUsersFromRoles(users, roles);
                }
                if (formValues["student"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)[0].Equals("true"))
                {
                    roles[0] = "student";
                    // save the User
                    result = repository.SaveUser(user);
                    Roles.AddUsersToRoles(users, roles);
                }
                else
                {
                    roles[0] = "student";
                    Roles.RemoveUsersFromRoles(users, roles);
                }
                if (formValues["ClassManagement"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)[0].Equals("true"))
                {
                    roles[0] = "ClassManagement";
                    // save the User
                    result = repository.SaveUser(user);
                    Roles.AddUsersToRoles(users, roles);
                }
                else
                {
                    roles[0] = "ClassManagement";
                    Roles.RemoveUsersFromRoles(users, roles);
                }
                if (formValues["CourseManagement"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)[0].Equals("true"))
                {
                    roles[0] = "CourseManagement";
                    // save the User
                    result = repository.SaveUser(user);
                    Roles.AddUsersToRoles(users, roles);
                }
                else
                {
                    roles[0] = "CourseManagement";
                    Roles.RemoveUsersFromRoles(users, roles);
                }
                if (formValues["GroupManagement"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)[0].Equals("true"))
                {
                    roles[0] = "GroupManagement";
                    // save the User
                    result = repository.SaveUser(user);
                    Roles.AddUsersToRoles(users, roles);
                }
                else
                {
                    roles[0] = "GroupManagement";
                    Roles.RemoveUsersFromRoles(users, roles);
                }
                if (formValues["SystemManagement"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)[0].Equals("true"))
                {
                    roles[0] = "SystemManagement";
                    // save the User
                    result = repository.SaveUser(user);
                    Roles.AddUsersToRoles(users, roles);
                }
                else
                {
                    roles[0] = "SystemManagement";
                    Roles.RemoveUsersFromRoles(users, roles);
                }
                if (formValues["TestManagement"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)[0].Equals("true"))
                {
                    roles[0] = "TestManagement";
                    // save the User
                    result = repository.SaveUser(user);
                    Roles.AddUsersToRoles(users, roles);
                }
                else
                {
                    roles[0] = "TestManagement";
                    Roles.RemoveUsersFromRoles(users, roles);
                }
                if (formValues["UserManagement"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)[0].Equals("true"))
                {
                    roles[0] = "UserManagement";
                    // save the User
                    result = repository.SaveUser(user);
                    Roles.AddUsersToRoles(users, roles);
                }
                else
                {
                    roles[0] = "UserManagement";
                    Roles.RemoveUsersFromRoles(users, roles);
                }

                // add a message to the viewbag
                if (result > 0)
                    TempData["message"] = string.Format("{0} has been saved", user.name);
                else
                    TempData["message"] = string.Format("Unsuccessful procedure!");

                // return the user to the list
                return RedirectToAction("Index");
            }
            else
            {
                // there is something wrong with the data values
                return View(user);
            }
        }