PageRenderTime 1936ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/IBOXWeb/IBOX/Modules/Library/Components/Users/UserController.cs

http://graduate-ndkhoiits.googlecode.com/
C# | 196 lines | 177 code | 5 blank | 14 comment | 8 complexity | a6019efb659e2024a3c3b451e4b8faf8 MD5 | raw file
  1. using System.Data;
  2. using Butterfly.Library.Components.Providers;
  3. using Butterfly.Library.Components.Security;
  4. using Butterfly.Library.Utilities;
  5. using System;
  6. namespace Butterfly.Library.Components.Users
  7. {
  8. public class UserController
  9. {
  10. private const string key = "Butterfly.Portal.User";
  11. public static UserInfo GetCurrentUser()
  12. {
  13. return DataAccessProvider.Instance().GetUser(AuthenticateUtility.GetUsername());
  14. }
  15. public static DataTable GetUsers()
  16. {
  17. //string param = "GetUsers";
  18. //DataCaching data = new DataCaching();
  19. DataTable dtUsers ;//= (DataTable) data.GetHashCache(key, param);
  20. //if (dtUsers != null) return dtUsers;
  21. dtUsers = DataAccessProvider.Instance().GetUsers();
  22. //data.SetHashCache(key, param, AppEnv.GetTimeCacheExpire(), dtUsers);
  23. return dtUsers;
  24. }
  25. public static DataTable GetUsers(int roleId, int portalId)
  26. {
  27. //string param = "GetUsers?roleid=" + roleId + "&portalId=" + portalId;
  28. //DataCaching data = new DataCaching();
  29. DataTable dtUsers;// = (DataTable)data.GetHashCache(key, param);
  30. //if (dtUsers != null) return dtUsers;
  31. dtUsers = DataAccessProvider.Instance().GetUsers(roleId, portalId);
  32. //data.SetHashCache(key, param, AppEnv.GetTimeCacheExpire(), dtUsers);
  33. return dtUsers;
  34. }
  35. public static DataTable GetUsers(string keyword, string searchby, string filter, int portalid)
  36. {
  37. return DataAccessProvider.Instance().GetUsers(keyword, searchby, filter, portalid);
  38. }
  39. public static DataTable GetUsersByUser(string keyword, string searchby, string filter, int userid, int portalid)
  40. {
  41. return DataAccessProvider.Instance().GetUsersByUser(keyword, searchby, filter, userid, portalid);
  42. }
  43. public static DataTable GetUsersNotInRole(int roleId, int portalId)
  44. {
  45. //string param = "GetUsersNotInRole?roleid=" + roleId + "&portalid=" + portalId;
  46. //DataCaching data = new DataCaching();
  47. DataTable dtUsers ;//= (DataTable)data.GetHashCache(key, param);
  48. //if (dtUsers != null) return dtUsers;
  49. dtUsers = DataAccessProvider.Instance().GetUsersNotInRole(roleId, portalId);
  50. //data.SetHashCache(key, param, AppEnv.GetTimeCacheExpire(), dtUsers);
  51. return dtUsers;
  52. }
  53. public static UserInfo ValidateUser(string username, string password)
  54. {
  55. return DataAccessProvider.Instance().ValidateUser(username, password);
  56. }
  57. public static int AddUser(UserInfo user)
  58. {
  59. int id = DataAccessProvider.Instance().AddUser(user);
  60. DataCaching data = new DataCaching();
  61. data.RemoveCache(key);
  62. return id;
  63. }
  64. public static void UpdateUser(UserInfo user)
  65. {
  66. DataAccessProvider.Instance().UpdateUser(user);
  67. DataCaching data = new DataCaching();
  68. data.RemoveCache(key);
  69. }
  70. public static void DeleteUser(int userId)
  71. {
  72. DataAccessProvider.Instance().DeleteUser(userId);
  73. DataCaching data = new DataCaching();
  74. data.RemoveCache(key);
  75. }
  76. public static UserInfo GetUser(int userId)
  77. {
  78. return DataAccessProvider.Instance().GetUser(userId);
  79. }
  80. public static UserInfo GetUser(string username)
  81. {
  82. return DataAccessProvider.Instance().GetUser(username);
  83. }
  84. public static void AddCommandToUser(int commandId, int userId)
  85. {
  86. DataAccessProvider.Instance().AddCommandToUser(commandId, userId);
  87. DataCaching data = new DataCaching();
  88. data.RemoveCache("Butterfly.Portal.Command.GetCommandsForUserNotGroup");
  89. }
  90. public static void RemoveCommandFromUser(int commandId, int userId)
  91. {
  92. DataAccessProvider.Instance().RemoveCommandFromUser(commandId, userId);
  93. }
  94. public static void ResetPassword(int userId, string password)
  95. {
  96. DataAccessProvider.Instance().ResetPassword(userId, password);
  97. DataCaching data = new DataCaching();
  98. data.RemoveCache(key);
  99. }
  100. public static void ChangePassword(int userId, string oldPassword, string newPassword)
  101. {
  102. DataAccessProvider.Instance().ChangePassword(userId, oldPassword, newPassword);
  103. DataCaching data = new DataCaching();
  104. data.RemoveCache(key);
  105. }
  106. public static void AddCommandToUserByPortalID(int commandId, int userId, int portalId)
  107. {
  108. DataAccessProvider.Instance().AddCommandToUserByPortalID(commandId, userId, portalId);
  109. DataCaching data = new DataCaching();
  110. data.RemoveCache("Butterfly.Portal.Command.GetCommandsForUserNotGroup");
  111. }
  112. public static void RemoveCommandFromUserByPortalID(int commandId, int userId, int portalId)
  113. {
  114. DataAccessProvider.Instance().RemoveCommandFromUserByPortalID(commandId, userId, portalId);
  115. }
  116. public static DataRow[] GetUserPermissionByParentID(DataTable dtCommand, int parentID)
  117. {
  118. DataRow[] retVal = null;
  119. retVal = dtCommand.Select("CommandParentID = " + parentID);
  120. return retVal;
  121. }
  122. public static DataTable BuildRecursiveUserPermission(DataTable dtCommand, DataTable dtReturnCommand,int userid, int portalid)
  123. {
  124. DataRow[] dtItems = GetUserPermissionByParentID(dtCommand, 0);
  125. foreach (DataRow row in dtItems)
  126. {
  127. DataRow rowItem = dtReturnCommand.NewRow();
  128. rowItem["CommandID"] = row["CommandID"].ToString();
  129. rowItem["PortalID"] = portalid.ToString();
  130. rowItem["CommandParentID"] = row["CommandParentID"].ToString();
  131. rowItem["UserID"] = userid.ToString();
  132. rowItem["CommandName"] = "<font color=\"Blue\">" + row["CommandName"].ToString() + "</font>";
  133. rowItem["Role_View"] = row["Role_View"].ToString();
  134. rowItem["Role_Add"] = row["Role_Add"].ToString();
  135. rowItem["Role_Update"] = row["Role_Update"].ToString();
  136. rowItem["Role_Delete"] = row["Role_Delete"].ToString();
  137. rowItem["Level"] = "0";
  138. dtReturnCommand.Rows.Add(rowItem);
  139. LoadUserPermissionForCur(dtCommand, rowItem, dtReturnCommand);
  140. }
  141. DataCaching data = new DataCaching();
  142. data.RemoveCache("Butterfly.Portal.Command.GetCommandsForUserNotGroup");
  143. return dtReturnCommand;
  144. }
  145. public static DataTable LoadUserPermissionForCur(DataTable dtCommand,DataRow curItem, DataTable dtReturnCommand)
  146. {
  147. int level = Convert.ToInt32(curItem["Level"]);
  148. level += 1;
  149. int _commandID = Convert.ToInt32(curItem["CommandID"]);
  150. DataRow[] dtChild = GetUserPermissionByParentID(dtCommand, _commandID);
  151. foreach (DataRow row in dtChild)
  152. {
  153. DataRow childItem = dtReturnCommand.NewRow();
  154. childItem["CommandID"] = row["CommandID"].ToString();
  155. childItem["CommandParentID"] = row["CommandParentID"].ToString();
  156. childItem["PortalID"] = row["PortalID"].ToString();
  157. childItem["UserID"] = row["UserID"].ToString();
  158. if (level == 1)
  159. {
  160. childItem["CommandName"] = MiscUtility.StringIndent(level) + "<font color=\"Red\">" + row["CommandName"].ToString() + "</font>";
  161. }
  162. else if (level == 2)
  163. {
  164. childItem["CommandName"] = MiscUtility.StringIndent(level) + "<font color=\"Orange\">" + row["CommandName"].ToString() + "</font>";
  165. }
  166. else if (level == 3)
  167. {
  168. childItem["CommandName"] = MiscUtility.StringIndent(level) + "<font color=\"Black\">" + row["CommandName"].ToString() + "</font>";
  169. }
  170. else
  171. {
  172. childItem["CommandName"] = MiscUtility.StringIndent(level) + "<font color=\"Gray\">" + row["CommandName"].ToString() + "</font>";
  173. }
  174. childItem["Role_View"] = row["Role_View"].ToString();
  175. childItem["Role_Add"] = row["Role_Add"].ToString();
  176. childItem["Role_Update"] = row["Role_Update"].ToString();
  177. childItem["Role_Delete"] = row["Role_Delete"].ToString();
  178. childItem["Level"] = level.ToString();
  179. dtReturnCommand.Rows.Add(childItem);
  180. LoadUserPermissionForCur(dtCommand, childItem, dtReturnCommand);
  181. }
  182. return dtReturnCommand;
  183. }
  184. public static void UpdateUserPermissionByPortalID(int userId,int commandId, int portalId, bool view, bool add, bool update, bool delete)
  185. {
  186. //DataCaching data = new DataCaching();
  187. //data.RemoveCache("Butterfly.Portal.Command.GetCommandsForUserNotGroup");
  188. DataAccessProvider.Instance().UpdateUserPermissionByPortalID(commandId, userId, portalId, view, add, update, delete);
  189. }
  190. }
  191. }