public override string[] GetRolesForUser(string username)
        {
            using (var usersContext = new DataContext())
            {
                UserModel user = new UserModel();
                try
                {
                    user = usersContext.Users.SingleOrDefault(u => u.Email == username);

                    if (user == null)
                        return new string[] { };
                    return user.UserRoles == null ? new string[] { } :
                      user.UserRoles.Select(u => u.Role).Select(u => u.RoleName).ToArray();
                }
                catch (Exception ex)
                {
                    //Ignore
                    //Problem occur only where the program is trying to
                    //recreate the user into the database in which the
                    //database will not allow it to happen.

                    //Return default
                    return user.UserRoles == null ? new string[] { } :
                      user.UserRoles.Select(u => u.Role).Select(u => u.RoleName).ToArray();

                }
            }
        }
 public ActionResult ChangePassword(UserModel user)
 {
     if (ModelState.IsValid)
     {
         db.Entry(user).State = EntityState.Modified;
         db.SaveChanges();
         return RedirectToAction("Detail", "User");
     }
     return View(user);
 }
        public ActionResult Create(UserModel usermodel)
        {
            if (ModelState.IsValid)
            {
                db.Users.Add(usermodel);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(usermodel);
        }
        public ActionResult CreateUser(UserModel user)
        {
            if (ModelState.IsValid)
            {

                using (var db = new DataContext())
                {
                    //By default all registration of any user will start off as a normal user till they decide to request for a operator role
                    //in manage a site/resort or both.
                    if (db.Users.FirstOrDefault(u => u.Email == user.Email) == null)
                    {
                        var crypto = new SimpleCrypto.PBKDF2();

                        var encryptPass = crypto.Compute(user.Password);

                        var u = new UserModel();

                        u.Email = user.Email;
                        u.Password = encryptPass;
                        u.PasswordSalt = crypto.Salt;
                        db.Users.Add(u);
                        db.SaveChanges();

                        UserRole UR = new UserRole();
                        UR.UserId = u.UserId;

                        //Get from User input upon creating a new User
                        //u.UserRoles = user.UserRoles;

                        //Role R = new Role();
                        //foreach (var role in db.Roles.ToList())
                        //{
                        //    if (role.RoleName == u.UserRoles.ToString())
                        //    {
                        //        R = role;
                        //        break;
                        //    }
                        //}

                        Role R = new Role();
                        foreach (var role in db.Roles.ToList())
                        {
                            if (role.RoleName == "Organization")
                            {
                                R = role;
                                break;
                            }
                        }

                        UR.RoleId = R.RoleId;
                        UR.Role = R;
                        db.UserRoles.Add(UR);
                        db.SaveChanges();

                        OrganizationModel info = new OrganizationModel();
                        db.OrganizationInfo.Add(info);
                        db.SaveChanges();

                        u.OrganizationId = info.OrganizationId;
                        db.SaveChanges();

                        return Login(user);
                    }
                }
            }
            else
            {
                ModelState.AddModelError("", "Data input is incorrect!");
            }
            return View();
        }
 public ActionResult Login(UserModel user)
 {
     if (ModelState.IsValid)
     {
         if (IsValid(user.Email, user.Password))
         {
             SetupFormsAuthTicket(user.Email, true);
             //FormsAuthentication.SetAuthCookie(user.Email, false);
             return RedirectToAction("Index", "Home");
         }
         else
         {
             ModelState.AddModelError("", "Login data is incorrect!");
         }
     }
     return View(user);
 }
        //working
        //
        // GET: /User/
        //---------------------Roles----------------------
        //User - normal user
        //Admin - manage users and the whole website
        //Organization - manage both the resort and the site
        //---------------------Roles----------------------
        //---------------------IMPORTANT NOTICE----------------------
        //---------------------Creation of Organization----------------------
        /*
         * How this is going to work:
         * Example:
         * STEP 1: Click on the button "Start-Up An Organization"(under the home screen in the normal user home page)
         * STEP 2: Then go through the process where the user will need to create an Organization under the creation page
         * STEP 3: After the creation is done then the user role will be changed from user to organization to indicate under UserRole and the user will have an organization ID under the user
         * STEP 4: AFter that then the organization user is able to add a DC(Dive Centre) or a DR(Dive Resort) under the organization
         */
        //---------------------Creation of Organization----------------------
        public ActionResult Index()
        {
            if (Roles.IsUserInRole("Administrator"))
            {
                return View(db.Users.ToList());
            }
            else if (Roles.IsUserInRole("Organization"))//in the mist of thinking whether to show all the users that belong to the organization
            {
                //Get the current CompanyProfileId
                //var currentUser = from c in db.Users.ToList()
                //                  where c.Email == User.Identity.Name
                //                  select c;

                UserModel CU = new UserModel();
                //foreach (var getUser in db.Users.ToList())
                //{
                //    if (getUser.Email == User.Identity.Name)
                //    {
                //        CU = getUser;
                //    }
                //}
               CU = db.Users.FirstOrDefault(u => u.Email == User.Identity.Name);

                //get the organization

                return RedirectToAction("Index", "Home", null);
            }
            else
            {
                return RedirectToAction("Index", "Home", null);
            }
        }
        public ActionResult Edit(UserModel usermodel)
        {
            //UserModel user = db.Users.Find(usermodel.UserID);
            //usermodel.Email = user.Email;
            //usermodel.Password = user.Password;

            if (ModelState.IsValid)
            {
                db.Entry(usermodel).State = EntityState.Modified;
                db.SaveChanges();

                if (User.IsInRole("Adminstrator"))
                {
                    return RedirectToAction("Index");
                }
                else
                {
                    return RedirectToAction("Details", new { email = User.Identity.Name });
                }
            }

            return View(usermodel);
        }
        public void CreateOrganization()
        {
            UserModel user = new UserModel();

            user.Email = "*****@*****.**";
            user.Password = "******";

            using (var db = new DataContext())
            {
                try
                {
                    if (db.Users.FirstOrDefault(u => u.Email == user.Email) == null)
                    {
                        var crypto = new SimpleCrypto.PBKDF2();

                        var encryptPass = crypto.Compute(user.Password);

                        var u = new UserModel();

                        u.Email = user.Email;
                        u.Password = encryptPass;
                        u.PasswordSalt = crypto.Salt;
                        db.Users.Add(u);
                        db.SaveChanges();

                        UserRole UR = new UserRole();
                        UR.UserId = u.UserId;

                        Role R = new Role();
                        foreach (var role in db.Roles.ToList())
                        {
                            if (role.RoleName == "Organization")
                            {
                                R = role;
                                break;
                            }
                        }

                        UR.RoleId = R.RoleId;
                        UR.Role = R;

                        db.UserRoles.Add(UR);
                        db.SaveChanges();
                    }
                }
                catch (Exception ex)
                {
                }
            }
        }
 public void AddUser(UserModel user)
 {
     Users.Add(user);
     SaveChanges();
 }