public object Delete(string id) { ////_logger.LogInformation(LoggingEvents.LogDb, "Delete group user"); //_actionLog.InsertActionLog("VIB_GROUP_USER", "Delete group user", null, null,false); try { AdPermission permission = _context.AdPermissions.SingleOrDefault(x => x.GroupUserCode == id); AdUserInGroup userInGroup = _context.AdUserInGroups.SingleOrDefault(x => x.GroupUserCode == id); if (permission != null || userInGroup != null) { //_logger.LogError(LoggingEvents.LogDb, "Delete group user fail"); //_actionLog.InsertActionLog("VIB_GROUP_USER", "Delete department/PC fail", null, null, "Delete"); return(Json(new JMessage() { Error = true, Title = String.Format(CommonUtil.ResourceValue("COM_ERR_OBJ_REF"), CommonUtil.ResourceValue("ADM_DEPARTMENT_LBL_DEPT").ToLower()) })); } else { var objChild = _context.AdGroupUsers.SingleOrDefault(x => x.ParentCode == id); if (objChild == null) { //AdGroupUser obj = new AdGroupUser(); //obj.GroupUserCode = id; //_context.AdGroupUsers.Attach(obj); var obj = _context.AdGroupUsers.FirstOrDefault(x => x.GroupUserId == Int32.Parse(id)); _context.AdGroupUsers.Remove(obj); _context.SaveChanges(); ////_logger.LogInformation(LoggingEvents.LogDb, "Delete group user successfully"); //_actionLog.InsertActionLog("VIB_GROUP_USER", "Delete department/PC successfully", obj, null, "Delete"); return(Json(new JMessage() { Error = false, Title = CommonUtil.ResourceValue("ADM_DEPARTMENT_MSG_DELETE_SUCCESS") })); } else { //_logger.LogError(LoggingEvents.LogDb, "Delete group user fail"); //_actionLog.InsertActionLog("VIB_GROUP_USER", "Delete department/PC fail", null, null, "Error"); return(Json(new JMessage() { Error = true, Title = String.Format(CommonUtil.ResourceValue("COM_MSG_DELETE_CHILD"), CommonUtil.ResourceValue("ADM_DEPARTMENT_LBL_DEPT")) })); } } } catch (Exception ex) { //_logger.LogError(LoggingEvents.LogDb, "Delete group user fail"); //_actionLog.InsertActionLog("VIB_GROUP_USER", "Delete department/PC failed: " + ex.Message, null, null, "Error"); return(Json(new JMessage() { Error = true, Title = String.Format(CommonUtil.ResourceValue("COM_MSG_DELETE_FAIL"), CommonUtil.ResourceValue("ADM_DEPARTMENT_LBL_DEPT").ToLower()) })); } }
protected bool UpdatePermissionUserByGroup(EIMDBContext context, string groupCode, string userId, string roleId, string appCode, string newRoleId = null, string newGroupCode = null, string newAppCode = null) { IQueryable <AdPermission> listPermissionDefault; if (newAppCode == null) { if (newRoleId == null) { if (newGroupCode == null || newGroupCode == groupCode) { listPermissionDefault = context.AdPermissions.Where(x => x.GroupUserCode == groupCode && x.UserId == null && x.RoleId == roleId && x.ApplicationCode == appCode); } else { // Remove old permission var listPermissionUser = context.AdPermissions.Where(x => x.GroupUserCode == groupCode && x.UserId != null && x.UserId == userId && x.RoleId == roleId && x.ApplicationCode == appCode); if (listPermissionUser.Any()) { context.RemoveRange(listPermissionUser); } // Get new default permission listPermissionDefault = context.AdPermissions.Where(x => x.GroupUserCode == newGroupCode && x.UserId == null && x.RoleId == roleId && x.ApplicationCode == appCode); } } else { // Remove old permission var listPermissionUser = context.AdPermissions.Where(x => x.GroupUserCode == groupCode && x.UserId != null && x.UserId == userId && x.RoleId == roleId && x.ApplicationCode == appCode); if (listPermissionUser.Any()) { context.RemoveRange(listPermissionUser); } if (newGroupCode == null || newGroupCode == groupCode) { // Get new default permission listPermissionDefault = context.AdPermissions.Where(x => x.GroupUserCode == groupCode && x.UserId == null && x.RoleId == newRoleId && x.ApplicationCode == appCode); } else { // Get new default permission listPermissionDefault = context.AdPermissions.Where(x => x.GroupUserCode == newGroupCode && x.UserId == null && x.RoleId == newRoleId && x.ApplicationCode == appCode); } } } else { if (newRoleId == null) { if (newGroupCode == null || newGroupCode == groupCode) { listPermissionDefault = context.AdPermissions.Where(x => x.GroupUserCode == groupCode && x.UserId == null && x.RoleId == roleId && x.ApplicationCode == newAppCode); } else { // Remove old permission var listPermissionUser = context.AdPermissions.Where(x => x.GroupUserCode == groupCode && x.UserId != null && x.UserId == userId && x.RoleId == roleId && x.ApplicationCode == appCode); if (listPermissionUser.Any()) { context.RemoveRange(listPermissionUser); } // Get new default permission listPermissionDefault = context.AdPermissions.Where(x => x.GroupUserCode == newGroupCode && x.UserId == null && x.RoleId == roleId && x.ApplicationCode == newAppCode); } } else { // Remove old permission var listPermissionUser = context.AdPermissions.Where(x => x.GroupUserCode == groupCode && x.UserId != null && x.UserId == userId && x.RoleId == roleId && x.ApplicationCode == appCode); if (listPermissionUser.Any()) { context.RemoveRange(listPermissionUser); } if (newGroupCode == null || newGroupCode == groupCode) { // Get new default permission listPermissionDefault = context.AdPermissions.Where(x => x.GroupUserCode == groupCode && x.UserId == null && x.RoleId == newRoleId && x.ApplicationCode == newAppCode); } else { // Get new default permission listPermissionDefault = context.AdPermissions.Where(x => x.GroupUserCode == newGroupCode && x.UserId == null && x.RoleId == newRoleId && x.ApplicationCode == newAppCode); } } } // Insert new permission of user if (listPermissionDefault.Any()) { foreach (var per in listPermissionDefault) { // Add new permission var permission = new AdPermission(); permission.ApplicationCode = per.ApplicationCode; permission.FunctionCode = per.FunctionCode; permission.ResourceCode = per.ResourceCode; permission.GroupUserCode = per.GroupUserCode; permission.RoleId = per.RoleId; permission.UserId = userId; context.AdPermissions.Add(permission); } } return(true); }
public async Task <IActionResult> UpdatePermission([FromBody] PermissionModel model) { JMessage msg = new JMessage { Error = true, Title = string.Format(CommonUtil.ResourceValue("MSG_UPDATE_FAIL"), CommonUtil.ResourceValue("PERMISSION").ToLower()) }; try { if (model.UserInGroups.Count > 0 && model.Resources.Count > 0) { foreach (var user in model.UserInGroups) { foreach (var resource in model.Resources) { var permission = await _context.AdPermissions.FirstOrDefaultAsync(x => x.ApplicationCode == model.ApplicationCode && x.FunctionCode == resource.FunctionCode && x.ResourceCode == resource.Code && x.GroupUserCode == user.GroupUserCode && x.UserId == user.UserId); if (permission == null) { if (resource.HasPermission) { // Add new permission permission = new AdPermission(); permission.ApplicationCode = model.ApplicationCode; permission.FunctionCode = resource.FunctionCode; permission.ResourceCode = resource.Code; permission.GroupUserCode = user.GroupUserCode; permission.UserId = user.UserId; permission.RoleId = user.RoleId; _context.AdPermissions.Add(permission); _actionLog.InsertActionLog("VIB_PERMISSION", "Add permission successfully", null, permission, "Insert"); } } else { if (resource.HasPermission) { // Update permission permission.RoleId = user.RoleId; _context.AdPermissions.Update(permission); _actionLog.InsertActionLog("VIB_PERMISSION", "Update permission successfully", null, permission, "Insert"); } else { _context.AdPermissions.Remove(permission); await _context.SaveChangesAsync(); } } } // Update role of user in group var userInGroup = await _context.AdUserInGroups.FirstOrDefaultAsync(x => x.GroupUserCode == user.GroupUserCode && x.UserId == user.UserId); if (userInGroup != null) { userInGroup.RoleId = user.RoleId; _context.AdUserInGroups.Update(userInGroup); } } var result = await _context.SaveChangesAsync(); //if (result > 0) //{ //_logger.LogInformation(LoggingEvents.LogDb, "Update permission success"); //_actionLog.InsertActionLog("VIBGroupUser", "Update permission success", objOld, model, "Update"); //} } msg.Error = false; msg.Title = string.Format(CommonUtil.ResourceValue("MSG_UPDATE_SUCCESS"), CommonUtil.ResourceValue("PERMISSION").ToLower()); //_actionLog.InsertActionLog("VIBPermissionResoure", "MSG_UPDATE_SUCCESS", null, model, "Insert"); } catch (Exception ex) { //_logger.LogError(LoggingEvents.LogDb, "Update permission failed"); _actionLog.InsertActionLog("VIB_PERMISSION", "Update permission failed", null, null, "Error"); msg.Object = ex; } return(Json(msg)); }