public static List<OrderItem> Get(User User, int StartIndex, int EndIndex) { SqlCommand SqlCommand = new SqlCommand(); SqlCommand.CommandText = "WITH ItemByPagger AS " + "( " + "SELECT " + " ROW_NUMBER() OVER( ORDER BY CreateTime DESC ) AS RowNumber , * " + "FROM " + " Order_OrderItem " + "WHERE " + " UserAccount = @UserAccount " + ") " + "SELECT " + " * " + "FROM " + " ItemByPagger " + "WHERE " + " RowNumber BETWEEN " + StartIndex + " AND " + EndIndex; SqlCommand.Parameters.AddWithValue("UserAccount", User.Account); return GetByDataReader(SqlCommand); }
//取得Recorders internal static List<SwitchUserLog> Select(User User) { List<SwitchUserLog> LoginRecoders = new List<SwitchUserLog>(); using (System.Data.SqlClient.SqlConnection SqlConnection = ConnectionManager.GetConnection()) { SqlCommand SqlCommand = SqlConnection.CreateCommand(); SqlCommand.CommandText = "SELECT " + " * " + "FROM " + " MemberShip_SwitchUserLog " + "WHERE " + " Account = @Account "; SqlCommand.Parameters.AddWithValue("Account", User.Account); SqlConnection.Open(); SqlDataReader SqlDataReader = SqlCommand.ExecuteReader(); while (SqlDataReader.Read()) { string Id = SqlDataReader["Id"].ToString(); string Account = SqlDataReader["Account"].ToString(); string Ip = SqlDataReader["Ip"].ToString(); string Remark = SqlDataReader["Remark"].ToString(); DateTime CreateTime = (DateTime)SqlDataReader["CreateTime"]; LoginRecoders.Add(new SwitchUserLog(Id, Account, Ip, Remark, CreateTime)); } } return LoginRecoders; }
//更新線上人員記錄 public static void UpdateLoginTag(User User) { if (LoginTagCache.ContainsKey(User) == false) { LoginTagCache.Add(User, GetNewLoginTag(User)); } OnlineTag OnlineTag = LoginTagCache[User]; OnlineTag.SessionId = HttpContext.Current.Session.SessionID; OnlineTag.ReflashTime = DateTime.Now; }
//建立一個新的登入紀錄(因為一次登入只會建立一筆登入紀錄,所以就不採用GetNew的方式撰寫了) private static SwitchUserLog CreateSwitchUserLog(User User, string Remark) { SwitchUserLog NewSwitchUserLog = new SwitchUserLog(Guid.NewGuid().ToString(), User.Account, HttpContext.Current.Request.UserHostAddress, Remark, DateTime.Now); //寫入資料庫,並限制最多保留3天內的紀錄就好 SwitchUserAccessor.InsertDelete(NewSwitchUserLog, 3); return NewSwitchUserLog; }
//取得新的LoginTag private static OnlineTag GetNewLoginTag(User User) { OnlineTag UserOnlineTag = new OnlineTag(); UserOnlineTag.Account = User.Account; UserOnlineTag.SessionId = HttpContext.Current.Session.SessionID; UserOnlineTag.ReflashTime = DateTime.Now; UserOnlineTag.CreateTime = DateTime.Now; return UserOnlineTag; }
public User(User Parent, string Account, string Password, List<RoleKey> Roles, Dictionary<ProfileKey, string> Profiles) { this.Parent = Parent; this.Account = Account; this.Password = Password; this.Roles = Roles; this.Profiles = Profiles; this.UpdateTime = DateTime.Now; this.CreateTime = DateTime.Now; }
//驗證是否重複登入 public static bool IsDuplicateLogin(User User) { if (User.Roles.Contains(RoleKey.Visitor)) { return false; } OnlineTag UserOnlineTag = null; if (LoginTagCache.TryGetValue(User, out UserOnlineTag) == false) { return false; } if (UserOnlineTag.SessionId != HttpContext.Current.Session.SessionID) { return true; } return false; }
//取得AllParent public static List<User> GetAllParent(User Child, bool WithSelf = true) { List<User> Parents = new List<User>(); if (WithSelf == true) { Parents.Add(Child); } FindAllParent(ref Parents, Child); return Parents; }
//找出所有的Parent private static void FindAllParent(ref List<User> Parents, User Child) { User Parent = Child.Parent; if (Parent == null) { return; } Parents.Add(Parent); FindAllParent(ref Parents, Parent); }
//根據RoleKey取得相關的AllChild public static List<User> GetAllChilds(User Child, RoleKey RoleKey, bool WithSelf = true) { List<User> Childs = new List<User>(); if (WithSelf == true) { Childs.Add(Child); } FindAllChilds(ref Childs, Child); return Childs.Where(c => c.Roles.Contains(RoleKey)).ToList(); }
//取得AllChild public static List<User> GetAllChilds(User Child, bool WithSelf = true) { List<User> Childs = new List<User>(); if (WithSelf == true) { Childs.Add(Child); } FindAllChilds(ref Childs, Child); return Childs; }
//找出所有的Child private static void FindAllChilds(ref List<User> ChildList, User Parent) { List<User> Childs = GetChilds(Parent); ChildList.AddRange(Childs); foreach (User Child in Childs) { FindAllChilds(ref ChildList, Child); } }
public static List<OrderItem> Get(User User) { SqlCommand SqlCommand = new SqlCommand(); SqlCommand.CommandText = "SELECT " + " * " + "FROM " + " Order_OrderItem " + "WHERE " + " UserAccount = @UserAccount " + "ORDER BY" + " CreateTime DESC"; SqlCommand.Parameters.AddWithValue("UserAccount", User.Account); return GetByDataReader(SqlCommand); }
//取得新的空User public static User GetNewUser(string Account, string Password) { if (UserCache.ContainsKey(Account) == true) { throw new Exception("帳號已經存在"); } User NewUser = new User(); NewUser.Account = Account; NewUser.Password = Password; NewUser.Roles = new List<RoleKey>(); NewUser.Roles.Add(RoleKey.Login); NewUser.Profiles = new Dictionary<ProfileKey, string>(); foreach (ProfileKey Type in Enum.GetValues(typeof(ProfileKey))) { NewUser.Profiles.Add(Type, ""); } NewUser.UpdateTime = DateTime.Now; NewUser.CreateTime = DateTime.Now; return NewUser; }
//取得線上人員紀錄透過User public static OnlineTag GetAllLoginTag(User User) { return LoginTagCache[User]; }
//初始化 public static void Initial() { Dictionary<string, string> Relations = new Dictionary<string, string>(); using (SqlConnection SqlConnection = ConnectionManager.GetConnection()) { SqlCommand SqlCommand = SqlConnection.CreateCommand(); SqlCommand.CommandText = "SELECT " + " * " + "FROM " + " MemberShip_User "; SqlConnection.Open(); SqlDataReader SqlDataReader = SqlCommand.ExecuteReader(); while (SqlDataReader.Read()) { User User = new User(); User.Parent = null; User.Account = SqlDataReader["Account"].ToString(); User.Password = LeftHand.Gadget.Encoder.AES_Decryption(SqlDataReader["Password"].ToString()); User.Roles = new List<RoleKey>(); string RoleString = SqlDataReader["Roles"].ToString(); if (RoleString != "") { User.Roles = RoleString.Split(',') .Select(r => (RoleKey)Enum.Parse(typeof(RoleKey), r)) .ToList(); } User.Profiles = new Dictionary<ProfileKey, string>(); string ProfileString = SqlDataReader["Profiles"].ToString(); if (ProfileString != "") { User.Profiles = ProfileString .Split(',') .ToDictionary(p => (ProfileKey)Enum.Parse(typeof(ProfileKey), p.Split(':')[0]), p => p.Split(':')[1]); } //將所有的ProfileKey都加上 foreach (ProfileKey ProfileKey in Enum.GetValues(typeof(ProfileKey))) { if (User.Profiles.Keys.Contains(ProfileKey) == false) { User.Profiles.Add(ProfileKey, ""); } } User.UpdateTime = (DateTime)SqlDataReader["UpdateTime"]; User.CreateTime = (DateTime)SqlDataReader["CreateTime"]; UserCache.Add(User.Account, User); Relations.Add(User.Account, SqlDataReader["Parent"].ToString()); } } //建立Parent關聯 foreach (KeyValuePair<string, string> Child_Parent in Relations) { string Child = Child_Parent.Key; string Parent = Child_Parent.Value; if (string.IsNullOrWhiteSpace(Parent)) { continue; } UserCache[Child].Parent = UserCache[Parent]; } }
//插入預設帳號資料 private static void CreateDeafultUser() { List<User> DefaultUsers = new List<User>(); User User1 = new User(); User1.Parent = null; User1.Account = "Visitor"; User1.Password = "******"; User1.Roles = new List<RoleKey>() { RoleKey.Visitor }; User1.Profiles = new Dictionary<ProfileKey, string>(); User1.Profiles.Add(ProfileKey.Name, "訪客"); User1.UpdateTime = DateTime.Now; User1.CreateTime = DateTime.Now; DefaultUsers.Add(User1); User User2 = new User(); User2.Parent = null; User2.Account = "MANAGER"; User2.Password = "******"; User2.Roles = new List<RoleKey>() { RoleKey.Login, RoleKey.Manager }; User2.Profiles = new Dictionary<ProfileKey, string>(); User2.Profiles.Add(ProfileKey.Name, "系統管理者"); User2.UpdateTime = DateTime.Now; User2.CreateTime = DateTime.Now; DefaultUsers.Add(User2); SaveUser(DefaultUsers); }
//儲存單一User public static void SaveUser(User User) { SaveUser(new List<User> { User }); }
//移除單一User public static void RemoveUser(User User) { RemoveUser(new List<User> { User }); }
//取得User的LoginRecoder public static List<SwitchUserLog> GetSwitchUserLog(User User) { return SwitchUserAccessor.Select(User); }
//取得Child public static List<User> GetChilds(User Parent) { return UserCache.Values.Where(u => u.Parent == Parent).ToList(); }
//發與驗證票 private static void PublishPassCard(User User) { HttpCookie PassCard = new HttpCookie("PassCard"); PassCard.HttpOnly = true; PassCard.Value = LeftHand.Gadget.Encoder.AES_Encryption(User.Account); //PassCard.Expires = DateTime.Now.AddDays(1); //註解的話關掉視窗就會登出 HttpContext.Current.Response.Cookies.Add(PassCard); }