public async Task <JsonResult> CountRequest(CountRequestGoogle obj) { var msg = new JMessage() { Error = false }; var DateTimeNow = DateTime.Now.AddHours(-14).ToString("MM/dd/yyyy"); try { var check = await _context.CountRequestGoogle.FirstOrDefaultAsync(x => x.Date == DateTimeNow && x.Service_type == obj.Service_type && x.Key == obj.Key); if (check == null) { obj.Date = DateTimeNow; obj.Create_time = DateTime.Now; obj.Update_time = DateTime.Now; _context.CountRequestGoogle.Add(obj); var a = await _context.SaveChangesAsync(); msg.Title = "Thêm khoản mục thành công"; msg.Object = obj; msg.ID = 1; } else { check.Num_request = check.Num_request + obj.Num_request; check.Is_limit = obj.Is_limit; check.Update_time = DateTime.Now; _context.CountRequestGoogle.Update(check); var x = await _context.SaveChangesAsync(); if (x > 0) { msg.ID = 1; msg.Title = "Update request success"; msg.Object = check; } else { msg.ID = 0; msg.Title = "Update request success"; msg.Object = check; } } } catch (Exception e) { msg.ID = 0; msg.Error = true; msg.Title = "Update request success"; msg.Object = e; } return(Json(msg)); }
public async Task <IActionResult> ChangePassword([FromBody] AccountLoginModel obj) { var msg = new JMessage { Error = false, Title = "" }; var us = await _context.Users.FirstOrDefaultAsync(x => x.Id == obj.Id); if (us != null) { var checkPassword = await _userManager.CheckPasswordAsync(us, obj.PasswordOld); if (checkPassword) { string code = await _userManager.GeneratePasswordResetTokenAsync(us); var result = await _userManager.ResetPasswordAsync(us, code, obj.PasswordNew); if (result.Succeeded) { var a = await _context.SaveChangesAsync(); msg.Title = "Cập nhập mật khẩu thành công"; } else { msg.Error = true; msg.Title = "Có lỗi khi cập nhập mật khẩu"; } } else { msg.Error = true; msg.Title = "Mật khẩu cũ nhập không đúng"; } } else { msg.Error = true; msg.Title = "Tài khoản không tồn tại"; } return(Json(msg)); }
public async Task <IActionResult> UpdateUserInOrg([FromBody] OrgUserModel model) { var msg = new JMessage() { Error = false }; try { if (string.IsNullOrEmpty(model.OrgAddonCode)) { msg.Error = true; msg.Title = string.Format(CommonUtil.ResourceValue("ERR_REQUIRED"), CommonUtil.ResourceValue("BRANCH").ToLower()); } else { var org = await _context.AdOrganizations.FirstOrDefaultAsync(x => x.OrgAddonCode == model.OrgAddonCode); if (org == null) { msg.Error = true; msg.Title = model.OrgAddonCode.StartsWith("d_") ? "This is Division, please select branch level!" : "Branch is not exists!"; } else { if (model.ListUser.Count > 0) { // Add user to branch var listAdd = _context.Users.Where(x => model.ListUser.Any(y => y == x.Id) && x.BranchId != model.OrgAddonCode); if (listAdd.Any()) { foreach (var user in listAdd) { if (user.BranchId == model.OrgAddonCode) { continue; } var oldBranch = user.BranchId; user.BranchId = model.OrgAddonCode; user.UpdatedDate = DateTime.Now; user.UpdatedBy = ESEIM.AppContext.UserName; _context.Update(user); //_actionLog.InsertActionLog("ASP_NET_USERS", "Update branch of user: "******"Update", false, user.UserName); } } // Remove user out branch var listDel = _context.Users.Where(x => model.ListUser.All(y => y != x.Id) && x.BranchId == model.OrgAddonCode); if (listDel.Any()) { foreach (var user in listDel) { var oldBranch = user.BranchId; user.BranchId = null; user.UpdatedDate = DateTime.Now; user.UpdatedBy = ESEIM.AppContext.UserName; _context.Update(user); //_actionLog.InsertActionLog("ASP_NET_USERS", "Update branch of user: "******"Update", false, user.UserName); } } } else { var listUser = _context.Users.Where(x => x.BranchId == model.OrgAddonCode); if (listUser.Any()) { foreach (var user in listUser) { var oldBranch = user.BranchId; user.BranchId = null; user.UpdatedDate = DateTime.Now; user.UpdatedBy = ESEIM.AppContext.UserName; _context.Update(user); //_actionLog.InsertActionLog("ASP_NET_USERS", "Update branch of user: "******"Update", false, user.UserName); } } } await _context.SaveChangesAsync(); msg.Title = string.Format(CommonUtil.ResourceValue("MSG_UPDATE_SUCCESS"), "tài khoản"); } } } catch (Exception e) { msg.Error = true; msg.Title = "Lỗi khi cập nhập"; _actionLog.InsertActionLog("ASP_NET_USERS", "Update branch of user failed: " + e.Message, null, null, "Error"); } return(Json(msg)); }
public async Task <JsonResult> UpdateUserPermission([FromBody] UserPerModel model) { JMessage msg = new JMessage { Error = true, Title = string.Format(CommonUtil.ResourceValue("MSG_UPDATE_FAIL"), CommonUtil.ResourceValue("PERMISSION").ToLower()) }; string userName = string.Empty; try { var user = await _context.Users.FirstOrDefaultAsync(x => x.Id == model.UserId); if (user != null) { var app = await _context.AdApplications.FirstOrDefaultAsync(x => x.ApplicationCode == model.AppCode); if (app != null) { // Update user is exceeded permission user.IsExceeded = model.IsExceeded; //// Update Branch Reference //var oldBranchRef = user.OrgReference; //user.OrgReference = model.BranchRefs.Count > 0 ? string.Join(",", model.BranchRefs) : ""; user.UpdatedDate = DateTime.Now; user.UpdatedBy = ESEIM.AppContext.UserName; _context.Update(user); //_actionLog.InsertActionLog("ASP_NET_USERS", "Update success branch reference of user: "******"Update", false, user.UserName); // Update Group User and Permission var listOldGroup = _context.AdUserInGroups.Where(x => x.UserId == user.Id && x.ApplicationCode == app.ApplicationCode).ToList(); var listOldPermissionAll = _context.AdPermissions.Where(x => x.ApplicationCode == app.ApplicationCode && x.UserId == user.Id && listOldGroup.Any(y => y.GroupUserCode == x.GroupUserCode)).ToList(); if (model.GroupUsers.Count > 0) { var listOldGroupDel = listOldGroup.Where(x => model.GroupUsers.All(y => y.GroupCode != x.GroupUserCode)).ToList(); _context.RemoveRange(listOldGroupDel); // Remove all old group not selected var listOldPermissionDel = listOldPermissionAll.Where(x => model.GroupUsers.All(y => y.GroupCode != x.GroupUserCode || (y.GroupCode == x.GroupUserCode && y.RoleId != x.RoleId))).ToList(); _context.RemoveRange(listOldPermissionDel); // Remove all old permission var listPermissionAll = _context.AdPermissions.Where(x => x.ApplicationCode == model.AppCode && model.GroupUsers.Any(y => y.GroupCode == x.GroupUserCode && y.RoleId == x.RoleId)).ToList(); var listPermissionDefault = listPermissionAll.Where(x => x.UserId == null).ToList(); var listPermissionUser = listPermissionAll.Where(x => x.UserId == model.UserId).ToList(); foreach (var groupUser in model.GroupUsers) { var oldGroup = listOldGroup.FirstOrDefault(x => x.GroupUserCode == groupUser.GroupCode && x.ApplicationCode == model.AppCode); var listPerDefaultGroup = listPermissionDefault.Where(x => x.GroupUserCode == groupUser.GroupCode && x.ApplicationCode == model.AppCode); if (oldGroup != null) { //if (groupUser.RoleId != oldGroup.RoleId) //{ // Update user in group oldGroup.BranchReference = model.BranchRefs.Count > 0 ? string.Join(",", model.BranchRefs) : ""; if (!oldGroup.IsMain) { oldGroup.RoleId = groupUser.RoleId; } oldGroup.GrantAll = true; _context.Update(oldGroup); // Update entity //} } else { // Add user to group var userInGroup = new AdUserInGroup(); userInGroup.UserId = model.UserId; userInGroup.GroupUserCode = groupUser.GroupCode; userInGroup.ApplicationCode = model.AppCode; userInGroup.RoleId = groupUser.RoleId; userInGroup.GrantAll = true; userInGroup.BranchReference = model.BranchRefs.Count > 0 ? string.Join(",", model.BranchRefs) : ""; _context.Add(userInGroup); // Add entity } // Add or Update permission if (groupUser.Resources != null && groupUser.Resources.Count > 0) { groupUser.Resources = groupUser.Resources.Where(x => x.HasPermission && !x.IsFunction).ToList(); // Get all permission need remove var permissionDel = listPermissionUser.Where(x => x.GroupUserCode == groupUser.GroupCode && x.RoleId == groupUser.RoleId && !groupUser.Resources.Any(y => y.FunctionCode == x.FunctionCode && y.Code == x.ResourceCode)); _context.RemoveRange(permissionDel); // Remove all permission not in selected // Get all permission need update var permissionUpdate = listPermissionUser.Where(x => x.GroupUserCode == groupUser.GroupCode && x.RoleId == groupUser.RoleId && groupUser.Resources.Any(y => y.FunctionCode == x.FunctionCode && y.Code == x.ResourceCode)).ToList(); if (permissionUpdate.Count > 0) { foreach (var perUpdate in permissionUpdate) { var resUpdate = groupUser.Resources.FirstOrDefault(y => y.FunctionCode == perUpdate.FunctionCode && y.Code == perUpdate.ResourceCode); perUpdate.ExpiredDate = resUpdate?.ExpiredDate; } } // Get all permission need add var permissionAdd = groupUser.Resources.Where(x => !listPermissionUser.Any(y => y.FunctionCode == x.FunctionCode && y.ResourceCode == x.Code && y.GroupUserCode == groupUser.GroupCode && y.RoleId == groupUser.RoleId)) .Select(x => new AdPermission { ApplicationCode = model.AppCode, FunctionCode = x.FunctionCode, ResourceCode = x.Code, GroupUserCode = groupUser.GroupCode, UserId = model.UserId, RoleId = groupUser.RoleId, ExpiredDate = x.ExpiredDate, }).ToList(); _context.AddRange(permissionAdd); // Add entity } else { //var permissionDel = listPermissionUser.Where(x => x.GroupUserCode == groupUser.GroupCode && x.RoleId == groupUser.RoleId && !listPerDefaultGroup.Any(y => y.FunctionCode == x.FunctionCode && y.ResourceCode == x.ResourceCode)); //_context.RemoveRange(permissionDel); // Remove all permission not in selected var permissionAdd = listPerDefaultGroup.Where(x => !listPermissionUser.Any(y => y.FunctionCode == x.FunctionCode && y.ResourceCode == x.ResourceCode && y.GroupUserCode == groupUser.GroupCode && y.RoleId == groupUser.RoleId)) .Select(x => new AdPermission { ApplicationCode = x.ApplicationCode, FunctionCode = x.FunctionCode, ResourceCode = x.ResourceCode, GroupUserCode = x.GroupUserCode, UserId = model.UserId, RoleId = x.RoleId, }).ToList(); _context.AddRange(permissionAdd); // Add entity } } } else { _context.RemoveRange(listOldGroup); // Remove all old group _context.RemoveRange(listOldPermissionAll); // Remove all old permission } //_actionLog.InsertActionLog("VIB_PERMISSION", "Update success permission for user: "******"Update", false, user.UserName); var result = await _context.SaveChangesAsync(); msg.Error = false; msg.Title = "Update user permission successfully"; } else { msg.Title = "Application is not exists in system!"; } } else { msg.Title = "User is not exists in system!"; } } catch (Exception ex) { //_actionLog.InsertActionLog("VIB_PERMISSION", "Update failed permission for user " + userName + " : " + ex.Message, null, null, "Error", true, userName); msg.Object = ex; } return(Json(msg)); }
public async Task <JsonResult> InsertFunction([FromBody] AppFuncModel obj) { var msg = new JMessage() { Error = false }; try { var app = _context.AdApplications.Where(p => p.ApplicationCode == obj.ApplicationCode).AsNoTracking().SingleOrDefault(); if (app != null) { // Add function if (obj.FunctionAdd != null && obj.FunctionAdd.Count > 0) { foreach (var funcCode in obj.FunctionAdd) { var function = await _context.AdFunctions.FirstOrDefaultAsync(x => x.FunctionCode == funcCode); if (function != null) { var appFunc = await _context.AdAppFunctions.FirstOrDefaultAsync(x => x.ApplicationCode == app.ApplicationCode && x.FunctionCode == funcCode); if (appFunc == null) { appFunc = new AdAppFunction(); appFunc.ApplicationCode = app.ApplicationCode; appFunc.FunctionCode = function.FunctionCode; _context.Add(appFunc); } } } } // Remove function if (obj.FunctionDel != null && obj.FunctionDel.Count > 0) { foreach (var funcCode in obj.FunctionDel) { var function = await _context.AdFunctions.FirstOrDefaultAsync(x => x.FunctionCode == funcCode); if (function != null) { var appFunc = await _context.AdAppFunctions.FirstOrDefaultAsync(x => x.ApplicationCode == app.ApplicationCode && x.FunctionCode == funcCode); if (appFunc != null) { _context.Remove(appFunc); } } } } await _context.SaveChangesAsync(); msg.Title = "Cập nhập chức năng cho ứng dụng thành công"; _actionLog.InsertActionLog("VIB_APP_FUNCTION", "Update function to application successfully", null, null, "Update"); } else { msg.Error = true; msg.Title = "Ứng dụng đã tồn tại!"; //_logger.LogError(LoggingEvents.LogDb, "Insert function fail"); } } catch (Exception ex) { msg.Error = true; msg.Title = String.Format(CommonUtil.ResourceValue("COM_MSG_ADD_FAIL"), CommonUtil.ResourceValue("FUNCTION")); //"Có lỗi khi thêm chức năng"; //_logger.LogError(LoggingEvents.LogDb, "Insert function fail"); _actionLog.InsertActionLog("VIB_APP_FUNCTION", "Update function to application failed: " + ex.Message, null, null, "Error"); } return(Json(msg)); }
public async Task <JsonResult> UpdatePermission([FromBody] PermissionModel model) { JMessage msg = new JMessage { Error = true, Title = string.Format(CommonUtil.ResourceValue("MSG_UPDATE_FAIL"), CommonUtil.ResourceValue("PERMISSION").ToLower()) }; try { model.Resources = model.Resources.Where(x => !x.IsFunction).ToList(); if (model.GroupCodes.Count > 0) { var listFunctionChild = await _context.AdFunctions.Where(x => x.FunctionCode == model.FunctionCode || x.ParentCode == model.FunctionCode || x.Parent.ParentCode == model.FunctionCode).ToListAsync(); var listGroupUser = await _context.AdGroupUsers.Where(x => model.GroupCodes.Any(y => y == x.GroupUserCode)).ToListAsync(); var listUserInGroup = await _context.AdUserInGroups.Where(x => model.GroupCodes.Any(y => y == x.GroupUserCode) && x.RoleId == model.RoleId).ToListAsync(); var listPermissionAll = await _context.AdPermissions.Where(x => x.ApplicationCode == model.ApplicationCode && x.RoleId == model.RoleId && listGroupUser.Any(y => y.GroupUserCode == x.GroupUserCode) && (string.IsNullOrEmpty(model.FunctionCode) || listFunctionChild.Any(y => y.FunctionCode == x.FunctionCode))).ToListAsync(); var listPermissionDefault = listPermissionAll.Where(x => x.UserId == null).ToList(); var listPermissionUser = listPermissionAll.Where(x => x.UserId != null).ToList(); if (listGroupUser.Count > 0) { foreach (var groupUser in listGroupUser) { if (!model.IsMultiple) { // Remove permission default var delPermissionDefault = listPermissionDefault.Where(x => x.GroupUserCode == groupUser.GroupUserCode && !model.Resources.Any(y => y.HasPermission && !y.IsFunction && y.FunctionCode == x.FunctionCode && y.Code == x.ResourceCode)); _context.RemoveRange(delPermissionDefault); // Remove permission user var delPermissionUser = listPermissionUser.Where(x => x.GroupUserCode == groupUser.GroupUserCode && !model.Resources.Any(y => y.HasPermission && !y.IsFunction && y.FunctionCode == x.FunctionCode && y.Code == x.ResourceCode)); _context.RemoveRange(delPermissionUser); } // Add permission default var addPermissionDefault = model.Resources.Where(x => x.HasPermission && !x.IsFunction && !listPermissionDefault.Any(y => y.GroupUserCode == groupUser.GroupUserCode && y.FunctionCode == x.FunctionCode && y.ResourceCode == x.Code)) .Select(x => new AdPermission { ApplicationCode = model.ApplicationCode, FunctionCode = x.FunctionCode, ResourceCode = x.Code, GroupUserCode = groupUser.GroupUserCode, RoleId = model.RoleId, UserId = null, }); _context.AddRange(addPermissionDefault); // Add permission user var listUser = listUserInGroup.Where(x => x.GroupUserCode == groupUser.GroupUserCode).ToList(); //var permissionUser = listPermissionUser.Where(x => x.GroupUserCode == groupUser.GroupUserCode).GroupBy(g => g.UserId).ToList(); if (listUser.Count > 0) { foreach (var perUser in listUser) { var addPermissionUser = model.Resources.Where(x => x.HasPermission && !x.IsFunction && x.Scope == false && !listPermissionUser.Any(y => y.GroupUserCode == groupUser.GroupUserCode && y.FunctionCode == x.FunctionCode && y.ResourceCode == x.Code)) .Select(x => new AdPermission { ApplicationCode = model.ApplicationCode, FunctionCode = x.FunctionCode, ResourceCode = x.Code, GroupUserCode = groupUser.GroupUserCode, RoleId = model.RoleId, UserId = perUser.UserId, }); _context.AddRange(addPermissionUser); } } } } var result = await _context.SaveChangesAsync(); } _actionLog.InsertActionLog("VIB_PERMISSION", "Update define permission for deparment/profit center success", null, null, "Update"); msg.Error = false; msg.Title = string.Format(CommonUtil.ResourceValue("MSG_UPDATE_SUCCESS"), CommonUtil.ResourceValue("PERMISSION").ToLower()); } catch (Exception ex) { _actionLog.InsertActionLog("VIB_PERMISSION", "Update define permission failed: " + ex.Message, null, null, "Error"); msg.Object = ex; } return(Json(msg)); }
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)); }