示例#1
0
        public DataTable GetAccount(string username)
        {
            if (!authHeader())
            {
                throw new Exception(StringResources.E00001);
            }

            AccountTableAdapter adapter = new AccountTableAdapter();
            DataTable dt = null;
            try
            {
                dt = adapter.GetDataByUsername(username);
                SqlCommand sc = adapter.Adapter.SelectCommand;
                return dt;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
示例#2
0
        public ActionResult ChangePassword(ChangePassword model)
        {
            if (!ModelState.IsValid)
            {
                return View();
            }

            string username = AccountInfo.GetUserName(Request);

            string oldPassword = model.OldPassword;
            string newPassword = model.Password;

            AccountTableAdapter adapter = new AccountTableAdapter();
            DataTable dt = adapter.GetDataByUsername(username);

            if (!dt.Rows[0]["PASSWORD"].ToString().Equals(oldPassword))
            {
                ModelState.AddModelError("", "Nhập sai mật khẩu cũ.");
                return View();
            }

            AccountTableAdapter AccountAdapter = new AccountTableAdapter();

            try
            {
                AccountAdapter.ChangePassword(newPassword, username);
                Log.ActivityLog("Account: " + username + "change password!");
            }
            catch (Exception ex)
            {
                Log.ErrorLog(ex.Message);
            }

            return RedirectToAction("HomePage", "Home");
        }
示例#3
0
        public ActionResult Login(LoginViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            string username = model.Username;
            string password = model.Password;
            bool remember = model.Remember;

            AccountTableAdapter adapter = new AccountTableAdapter();
            DataTable dt = adapter.GetDataByUsername(username);

            if (dt.Rows.Count != 1)
            {
                ModelState.AddModelError("", "Tên đăng nhập không tồn tại.");
                return View(model);
            }

            if ((string)dt.Rows[0]["PASSWORD"] != password)
            {
                ModelState.AddModelError("", "Sai mật khẩu.");
                return View(model);
            }

            try
            {
                string role = AccountInfo.GetRoleNameEnglish(dt.Rows[0].Field<int>("Role"));
                FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
                1,
                username,
                DateTime.Now,
                DateTime.Now.AddDays(20),
                remember,
                role
                );
                HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(ticket));
                string cookieName = FormsAuthentication.FormsCookieName;
                string cookieValue = FormsAuthentication.Encrypt(ticket);
                cookie.Path = FormsAuthentication.FormsCookiePath;

                if (remember)
                {
                    cookie.Expires = ticket.Expiration;
                }

                HttpContext.Response.Cookies.Set(cookie);
            }
            catch (Exception ex)
            {
                Log.ErrorLog(ex.Message);
            }

            return RedirectToAction("HomePage", "Home");
        }
示例#4
0
        public ActionResult UserInfo()
        {
            string username = AccountInfo.GetUserName(Request);

            UserinfoModel userinfo = new UserinfoModel();
            DataTable userInfoDataTable = new DataTable();
            UserInfoDetailTableAdapter userInfoDetailAdapter = new UserInfoDetailTableAdapter();
            UserInfoTableAdapter userInfoAdapter = new UserInfoTableAdapter();
            userInfoDataTable = userInfoAdapter.GetDataByUsername(username);
            string userTypeName = null;
            string email = null;
            if (!string.IsNullOrEmpty(userInfoDataTable.Rows[0]["TypeShortName"].ToString()))
            {
                userInfoDataTable = userInfoDetailAdapter.GetDataByUsername(username);
                userTypeName = (string)userInfoDataTable.Rows[0]["TypeName"];
                email = userInfoDataTable.Rows[0]["Email"].ToString();
            }
            else
            {
                AccountTableAdapter accountAdapter = new AccountTableAdapter();
                DataTable accountDataTable = new DataTable();
                accountDataTable = accountAdapter.GetDataByUsername(username);
                email = accountDataTable.Rows[0]["Email"].ToString();
            }
            DateTime date = DateTime.Parse(userInfoDataTable.Rows[0]["LastUpdatedMoney"].ToString());
            int amountOfMoney = (int)userInfoDataTable.Rows[0]["AmountOfMoney"];

            TransactionHistoryTableAdapter transactionAdapter = new TransactionHistoryTableAdapter();
            int? money = transactionAdapter.GetCurrentMoney(username, date);
            if (money == null)
            {
                money = 0;
            }

            amountOfMoney += money.Value;

            userinfo.Username = username;
            userinfo.Name = userInfoDataTable.Rows[0]["Name"].ToString();
            userinfo.TypeName = userTypeName;
            userinfo.Email = email;
            userinfo.AmountOfMoney = amountOfMoney;

            return View(userinfo);
        }
示例#5
0
        public ActionResult EditUser(EditUserModel model)
        {
            UserTypeTableAdapter userTypeAdapter = new UserTypeTableAdapter();
            DataTable userTypeDT = userTypeAdapter.GetData();

            List<SelectListItem> items = new List<SelectListItem>();
            foreach (DataRow row in userTypeDT.Rows)
            {
                items.Add(new SelectListItem { Text = row["TypeName"].ToString(), Value = row["TypeShortName"].ToString() });
            }
            ViewData["UserType"] = items;

            if (!ModelState.IsValid)
            {
                return View(model);
            }

            string email = model.Email;
            if (!string.IsNullOrEmpty(email))
            {
                AccountTableAdapter AccountAdapter = new AccountTableAdapter();
                DataTable dt = AccountAdapter.GetDataByUsername(model.Username);

                if (!dt.Rows[0]["Email"].ToString().Equals(email))
                {
                    DataTable AccountDT = AccountAdapter.GetDataByEmail(email);

                    if (AccountDT.Rows.Count == 1)
                    {
                        if (!AccountDT.Rows[0]["Username"].ToString().Equals(model.Username))
                        {
                            Log.ActivityLog(AccountDT.Rows[0]["Username"].ToString().Equals(model.Username).ToString());
                            ModelState.AddModelError("", "Email đã tồn tại");
                            return View(model);
                        }
                    }
                }
            }

            string updateBy = AccountInfo.GetUserName(Request);
            DateTime date = DateTime.Now;
            string username = model.Username;
            string name = model.Name;
            string userTypeID = model.UserTypeID;
            bool isCafeteriaStaff = false;
            int role = model.Role;
            bool isActive = model.isActive;
            if (role == 2)
            {
                isCafeteriaStaff = true;
            }

            AccountTableAdapter accountAdapter = new AccountTableAdapter();
            UserInfoTableAdapter userInfoAdapter = new UserInfoTableAdapter();

            DataTable userInfoDT = userInfoAdapter.GetDataByUsername(username);
            DataRow userInfoRow = userInfoDT.Rows[0];

            int amountOfMoney = userInfoRow.Field<int>("AmountOfMoney");
            DateTime lastUpdatedMoney = userInfoRow.Field<DateTime>("LastUpdatedMoney");
            byte[] fingerPrintIMG = userInfoRow.Field<byte[]>("FingerPrintIMG");
            DateTime? lastUpdatedFingerPrint = userInfoRow.Field<DateTime?>("LastUpdatedFingerPrint");
            int? fingerPosition = userInfoRow.Field<int?>("FingerPosition");
            DateTime insertedDate = userInfoRow.Field<DateTime>("InsertedDate");

            accountAdapter.Connection.Open();
            userInfoAdapter.Connection = accountAdapter.Connection;

            using (SqlTransaction transaction = accountAdapter.Connection.BeginTransaction())
            {
                accountAdapter.AttachTransaction(transaction);
                userInfoAdapter.AttachTransaction(transaction);

                try
                {
                    userInfoAdapter.UpdateUserInfo(username, name, userTypeID, amountOfMoney, lastUpdatedMoney, fingerPrintIMG
                        , lastUpdatedFingerPrint, fingerPosition, isCafeteriaStaff, isActive, insertedDate, updateBy, date, username);
                    Log.ActivityLog("Update to UserInfo: username = "******"Update to Account: username = "******"editUser"] = "******";
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    Log.ErrorLog(ex.Message);
                    Session["editUser"] = "******";
                }
            }

            return RedirectToAction("EditUser", "Account", new { @username = model.Username });
        }