//
        // GET: /Manage/Index
        public async Task <ActionResult> Index(ManageMessageId?message)
        {
            ViewBag.StatusMessage =
                message == ManageMessageId.ChangePasswordSuccess ? "Your password has been changed."
                : message == ManageMessageId.SetPasswordSuccess ? "Your password has been set."
                : message == ManageMessageId.SetTwoFactorSuccess ? "Your two-factor authentication provider has been set."
                : message == ManageMessageId.Error ? "An error has occurred."
                : message == ManageMessageId.AddPhoneSuccess ? "Your phone number was added."
                : message == ManageMessageId.RemovePhoneSuccess ? "Your phone number was removed."
                : "";

            FM_Datastore_Entities_EF db_manager = new FM_Datastore_Entities_EF();
            long notification = db_manager.Notifications.LongCount();

            db_manager.Dispose();

            var userId = long.Parse(User.Identity.GetUserId());

            var model = new IndexViewModel
            {
                HasPassword       = HasPassword(),
                PhoneNumber       = await UserManager.GetPhoneNumberAsync(userId),
                TwoFactor         = await UserManager.GetTwoFactorEnabledAsync(userId),
                Logins            = await UserManager.GetLoginsAsync(userId),
                BrowserRemembered = await AuthenticationManager.TwoFactorBrowserRememberedAsync(userId.ToString()),
                Notifications     = notification
            };


            return(View(model));
        }
        public ActionResult NotifyView()
        {
            FM_Datastore_Entities_EF db_manager = new FM_Datastore_Entities_EF();
            var notifyListDB = db_manager.Notifications.ToList();

            // get division list
            List <SelectListItem> RolesList = new List <SelectListItem>();

            new SelectList(db_manager.Addresses, "Id", "addressLine1");
            foreach (var item in db_manager.Roles.ToList())
            {
                RolesList.Add(new SelectListItem()
                {
                    Text  = item.Name,
                    Value = item.Id.ToString() //  will be used to get id later
                });
            }
            long?roleResult = db_manager.Roles.FirstOrDefault(m => m.Name == AppSettings.Roles.APPROVEDUSER).Id;

            db_manager.Dispose();
            Session.Add("notifyListDB", notifyListDB);
            Session.Add("roleResult", roleResult);
            Session.Add("RolesList", RolesList);
            return(View(new NotifyViewModel()
            {
                Role = roleResult,
                Roles = RolesList,
                notifyList = notifyListDB
            }));
        }
        public ActionResult RegisterRequest()
        {
            FM_Datastore_Entities_EF db_manager = new FM_Datastore_Entities_EF();

            // get division list
            List <SelectListItem> divisionList = new List <SelectListItem>();

            new SelectList(db_manager.Addresses, "Id", "addressLine1");
            foreach (var item in db_manager.Divisions.ToList())
            {
                divisionList.Add(new SelectListItem()
                {
                    Text  = item.name,
                    Value = item.Id.ToString() //  will be used to get id later
                });
            }

            // get address list
            List <SelectListItem> addressList = new List <SelectListItem>();

            foreach (var item in db_manager.Addresses.ToList())
            {
                addressList.Add(new SelectListItem()
                {
                    Text  = item.city + ", " + item.state,
                    Value = item.Id.ToString() //  will be used to get id later
                });
            }
            db_manager.Dispose(); // no need to save changes
            return(View(new RegisterRequestViewModel()
            {
                addresses = addressList,
                divisions = divisionList
            }));
        }
        public ActionResult Register()
        {
            //Only allow people to view register page if they have a valid link
            NameValueCollection query = Request.QueryString;

            string[] qresult = query.GetValues("rqst");
            if (qresult == null || qresult.Length < 1)
            {
                return(new HttpNotFoundResult());
            }

            UrlEncryption EncryptionResult = UrlEncryption.Decrypt(qresult[0]);

            if (EncryptionResult == null || EncryptionResult.timeStamp > DateTime.UtcNow.AddHours(3)) // if null or url was created more than 3 hours ago dont accept
            {
                return(new HttpNotFoundResult());
            }

            FM_Datastore_Entities_EF db_manager = new FM_Datastore_Entities_EF();
            Role     RoleResult     = db_manager.Roles.FirstOrDefault(m => m.Id == EncryptionResult.role);
            Address  AddressResult  = db_manager.Addresses.FirstOrDefault(m => m.Id == EncryptionResult.address);
            Division DivisionResult = db_manager.Divisions.FirstOrDefault(m => m.Id == EncryptionResult.division);

            db_manager.Dispose();

            // store ids in session
            Session.Add("RoleResult", RoleResult.Id);
            Session.Add("AddressResult", AddressResult.Id);
            Session.Add("DivisionResult", DivisionResult.Id);
            return(View(new RegisterViewModel()
            {
                Email = EncryptionResult.email,
                Role = RoleResult.Name,
                Address = AddressResult.country + ": "
                          + AddressResult.addressLine1
                          + AddressResult.addressLine2
                          + ", " + AddressResult.city
                          + ", " + AddressResult.state
                          + ", " + AddressResult.postalCode,
                Division = DivisionResult.name
            }));
        }
        public ActionResult Notify(NotifyViewModel model, string submitButton, string id, long?Role)
        {
            if (User.IsInRole(AppSettings.Roles.APPROVEDUSER) || User.IsInRole(AppSettings.Roles.AUDITORS))
            {
                return(new HttpNotFoundResult());
            }

            if (model.Role == null)
            {
                return(View(model)); // redisplay the view if error
            }

            long role = (long)model.Role;

            if (Role != null)
            {
                role = (long)Role;
            }

            model.notifyList = (List <Notification>)Session["notifyListDB"];
            model.Role       = (long)Session["roleResult"];
            model.Roles      = (List <SelectListItem>)Session["RolesList"];
            long result;

            if (!long.TryParse(id, out result))
            {
                return(View(model));
            }

            FM_Datastore_Entities_EF db_manager = new FM_Datastore_Entities_EF();

            // get notification
            Notification oldNotify = db_manager.Notifications.FirstOrDefault(m => m.Id == result);

            switch (submitButton)
            {
            case "Resend Notification":
                //send email to new user
                Mail.send(
                    oldNotify.Email,
                    "Access Approved",
                    "here is the link to sign up this link will only be available for so long - "
                    + "https://"
                    + HttpContext.Request.Url.Authority
                    + Url.Action("Register", "Account")
                    + "?rqst="
                    + UrlEncryption.Encrypt(
                        DateTime.UtcNow,
                        oldNotify.Email,
                        oldNotify.AddressId,
                        oldNotify.DivisionId,
                        role));
                ViewBagHelper.setMessage(ViewBag, ViewBagHelper.MessageType.SuccessMsgBox, "New user request resent to \"" + oldNotify.Email + "\"");
                return(NotifyView());

            case "Accept":
                if (oldNotify.notifyType.Equals(AppSettings.Notify.newUser))
                {
                    //send email to new user
                    Mail.send(
                        oldNotify.Email,
                        "Access Approved",
                        "here is the link to sign up this link will only be available for so long - "
                        + "https://"
                        + HttpContext.Request.Url.Authority
                        + Url.Action("Register", "Account")
                        + "?rqst="
                        + UrlEncryption.Encrypt(
                            DateTime.UtcNow,
                            oldNotify.Email,
                            oldNotify.AddressId,
                            oldNotify.DivisionId,
                            role));
                    oldNotify.notifyType = AppSettings.Notify.pendingUser;
                    oldNotify.Role       = db_manager.Roles.FirstOrDefault(m => m.Id == role).Name;
                    db_manager.Entry(oldNotify);
                    db_manager.SaveChanges();
                    db_manager.Dispose();
                }
                return(NotifyView());

            case "Deny":
                // send denial email to user
                Mail.send(oldNotify.Email, "Denied Access", "Appologies user you have been denied access by administration to the application.");

                model.notifyList.Remove(model.notifyList.First(m => m.Id == result));     // remove from current model
                db_manager.Notifications.Remove(oldNotify);
                break;

            default:
                break;
            }

            db_manager.SaveChanges();
            db_manager.Dispose();
            return(View(model));
        }