/IBOXWeb/IBOX/Modules/Library/Components/Users/UserController.cs
C# | 196 lines | 177 code | 5 blank | 14 comment | 8 complexity | a6019efb659e2024a3c3b451e4b8faf8 MD5 | raw file
- using System.Data;
- using Butterfly.Library.Components.Providers;
- using Butterfly.Library.Components.Security;
- using Butterfly.Library.Utilities;
- using System;
-
- namespace Butterfly.Library.Components.Users
- {
- public class UserController
- {
- private const string key = "Butterfly.Portal.User";
- public static UserInfo GetCurrentUser()
- {
- return DataAccessProvider.Instance().GetUser(AuthenticateUtility.GetUsername());
- }
- public static DataTable GetUsers()
- {
- //string param = "GetUsers";
- //DataCaching data = new DataCaching();
- DataTable dtUsers ;//= (DataTable) data.GetHashCache(key, param);
- //if (dtUsers != null) return dtUsers;
- dtUsers = DataAccessProvider.Instance().GetUsers();
- //data.SetHashCache(key, param, AppEnv.GetTimeCacheExpire(), dtUsers);
- return dtUsers;
- }
-
- public static DataTable GetUsers(int roleId, int portalId)
- {
- //string param = "GetUsers?roleid=" + roleId + "&portalId=" + portalId;
- //DataCaching data = new DataCaching();
- DataTable dtUsers;// = (DataTable)data.GetHashCache(key, param);
- //if (dtUsers != null) return dtUsers;
- dtUsers = DataAccessProvider.Instance().GetUsers(roleId, portalId);
- //data.SetHashCache(key, param, AppEnv.GetTimeCacheExpire(), dtUsers);
- return dtUsers;
- }
- public static DataTable GetUsers(string keyword, string searchby, string filter, int portalid)
- {
- return DataAccessProvider.Instance().GetUsers(keyword, searchby, filter, portalid);
- }
- public static DataTable GetUsersByUser(string keyword, string searchby, string filter, int userid, int portalid)
- {
- return DataAccessProvider.Instance().GetUsersByUser(keyword, searchby, filter, userid, portalid);
- }
- public static DataTable GetUsersNotInRole(int roleId, int portalId)
- {
- //string param = "GetUsersNotInRole?roleid=" + roleId + "&portalid=" + portalId;
- //DataCaching data = new DataCaching();
- DataTable dtUsers ;//= (DataTable)data.GetHashCache(key, param);
- //if (dtUsers != null) return dtUsers;
- dtUsers = DataAccessProvider.Instance().GetUsersNotInRole(roleId, portalId);
- //data.SetHashCache(key, param, AppEnv.GetTimeCacheExpire(), dtUsers);
- return dtUsers;
- }
- public static UserInfo ValidateUser(string username, string password)
- {
- return DataAccessProvider.Instance().ValidateUser(username, password);
- }
- public static int AddUser(UserInfo user)
- {
- int id = DataAccessProvider.Instance().AddUser(user);
- DataCaching data = new DataCaching();
- data.RemoveCache(key);
- return id;
- }
- public static void UpdateUser(UserInfo user)
- {
- DataAccessProvider.Instance().UpdateUser(user);
- DataCaching data = new DataCaching();
- data.RemoveCache(key);
- }
- public static void DeleteUser(int userId)
- {
- DataAccessProvider.Instance().DeleteUser(userId);
- DataCaching data = new DataCaching();
- data.RemoveCache(key);
- }
- public static UserInfo GetUser(int userId)
- {
- return DataAccessProvider.Instance().GetUser(userId);
- }
- public static UserInfo GetUser(string username)
- {
- return DataAccessProvider.Instance().GetUser(username);
- }
- public static void AddCommandToUser(int commandId, int userId)
- {
- DataAccessProvider.Instance().AddCommandToUser(commandId, userId);
- DataCaching data = new DataCaching();
- data.RemoveCache("Butterfly.Portal.Command.GetCommandsForUserNotGroup");
- }
- public static void RemoveCommandFromUser(int commandId, int userId)
- {
- DataAccessProvider.Instance().RemoveCommandFromUser(commandId, userId);
- }
- public static void ResetPassword(int userId, string password)
- {
- DataAccessProvider.Instance().ResetPassword(userId, password);
- DataCaching data = new DataCaching();
- data.RemoveCache(key);
- }
- public static void ChangePassword(int userId, string oldPassword, string newPassword)
- {
- DataAccessProvider.Instance().ChangePassword(userId, oldPassword, newPassword);
- DataCaching data = new DataCaching();
- data.RemoveCache(key);
- }
- public static void AddCommandToUserByPortalID(int commandId, int userId, int portalId)
- {
- DataAccessProvider.Instance().AddCommandToUserByPortalID(commandId, userId, portalId);
- DataCaching data = new DataCaching();
- data.RemoveCache("Butterfly.Portal.Command.GetCommandsForUserNotGroup");
- }
- public static void RemoveCommandFromUserByPortalID(int commandId, int userId, int portalId)
- {
- DataAccessProvider.Instance().RemoveCommandFromUserByPortalID(commandId, userId, portalId);
- }
- public static DataRow[] GetUserPermissionByParentID(DataTable dtCommand, int parentID)
- {
- DataRow[] retVal = null;
- retVal = dtCommand.Select("CommandParentID = " + parentID);
- return retVal;
- }
- public static DataTable BuildRecursiveUserPermission(DataTable dtCommand, DataTable dtReturnCommand,int userid, int portalid)
- {
- DataRow[] dtItems = GetUserPermissionByParentID(dtCommand, 0);
- foreach (DataRow row in dtItems)
- {
- DataRow rowItem = dtReturnCommand.NewRow();
- rowItem["CommandID"] = row["CommandID"].ToString();
- rowItem["PortalID"] = portalid.ToString();
- rowItem["CommandParentID"] = row["CommandParentID"].ToString();
- rowItem["UserID"] = userid.ToString();
- rowItem["CommandName"] = "<font color=\"Blue\">" + row["CommandName"].ToString() + "</font>";
- rowItem["Role_View"] = row["Role_View"].ToString();
- rowItem["Role_Add"] = row["Role_Add"].ToString();
- rowItem["Role_Update"] = row["Role_Update"].ToString();
- rowItem["Role_Delete"] = row["Role_Delete"].ToString();
- rowItem["Level"] = "0";
-
- dtReturnCommand.Rows.Add(rowItem);
- LoadUserPermissionForCur(dtCommand, rowItem, dtReturnCommand);
- }
- DataCaching data = new DataCaching();
- data.RemoveCache("Butterfly.Portal.Command.GetCommandsForUserNotGroup");
- return dtReturnCommand;
- }
- public static DataTable LoadUserPermissionForCur(DataTable dtCommand,DataRow curItem, DataTable dtReturnCommand)
- {
- int level = Convert.ToInt32(curItem["Level"]);
- level += 1;
- int _commandID = Convert.ToInt32(curItem["CommandID"]);
- DataRow[] dtChild = GetUserPermissionByParentID(dtCommand, _commandID);
- foreach (DataRow row in dtChild)
- {
- DataRow childItem = dtReturnCommand.NewRow();
- childItem["CommandID"] = row["CommandID"].ToString();
- childItem["CommandParentID"] = row["CommandParentID"].ToString();
- childItem["PortalID"] = row["PortalID"].ToString();
- childItem["UserID"] = row["UserID"].ToString();
- if (level == 1)
- {
- childItem["CommandName"] = MiscUtility.StringIndent(level) + "<font color=\"Red\">" + row["CommandName"].ToString() + "</font>";
- }
- else if (level == 2)
- {
- childItem["CommandName"] = MiscUtility.StringIndent(level) + "<font color=\"Orange\">" + row["CommandName"].ToString() + "</font>";
- }
- else if (level == 3)
- {
- childItem["CommandName"] = MiscUtility.StringIndent(level) + "<font color=\"Black\">" + row["CommandName"].ToString() + "</font>";
- }
- else
- {
- childItem["CommandName"] = MiscUtility.StringIndent(level) + "<font color=\"Gray\">" + row["CommandName"].ToString() + "</font>";
- }
- childItem["Role_View"] = row["Role_View"].ToString();
- childItem["Role_Add"] = row["Role_Add"].ToString();
- childItem["Role_Update"] = row["Role_Update"].ToString();
- childItem["Role_Delete"] = row["Role_Delete"].ToString();
- childItem["Level"] = level.ToString();
-
- dtReturnCommand.Rows.Add(childItem);
- LoadUserPermissionForCur(dtCommand, childItem, dtReturnCommand);
- }
- return dtReturnCommand;
- }
- public static void UpdateUserPermissionByPortalID(int userId,int commandId, int portalId, bool view, bool add, bool update, bool delete)
- {
- //DataCaching data = new DataCaching();
- //data.RemoveCache("Butterfly.Portal.Command.GetCommandsForUserNotGroup");
- DataAccessProvider.Instance().UpdateUserPermissionByPortalID(commandId, userId, portalId, view, add, update, delete);
- }
- }
-
- }