public bool GetAPIKey(int userId, int id, out Library.DTO.Notification notification) { notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; try { using (User2MngEntities context = CreateContext()) { UserProfile dbItem = context.UserProfile.FirstOrDefault(o => o.UserId == id); if (dbItem == null) { throw new Exception("User not found!"); } else { dbItem.UpdatedBy = userId; dbItem.UpdatedDate = DateTime.Now; dbItem.APIKey = System.Guid.NewGuid().ToString().ToUpper(); context.SaveChanges(); return(true); } } } catch (Exception ex) { notification.Type = Library.DTO.NotificationType.Error; notification.Message = Library.Helper.HandleExceptionSingleLine(ex); return(false); } }
public bool ForceChangePassword(int userId, int id, out Library.DTO.Notification notification) { notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; try { using (User2MngEntities context = CreateContext()) { UserProfile dbItem = context.UserProfile.FirstOrDefault(o => o.UserId == id); if (dbItem == null) { throw new Exception("User not found!"); } else { dbItem.IsForcedToChangePassword = true; context.SaveChanges(); return(true); } } } catch (Exception ex) { notification = new Library.DTO.Notification() { Message = ex.Message, Type = Library.DTO.NotificationType.Error }; return(false); } }
public bool RestoreUser(int userId, int id, out Library.DTO.Notification notification) { notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; try { using (User2MngEntities context = CreateContext()) { UserProfile dbItem = context.UserProfile.FirstOrDefault(o => o.UserId == id); if (dbItem == null) { notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Error, Message = "Not found id" }; return(false); } else { dbItem.UpdatedBy = userId; dbItem.UpdatedDate = DateTime.Now; dbItem.IsActivated = true; dbItem.InActiveDate = null; Employee dbEmployee = context.Employee.FirstOrDefault(o => o.UserID.HasValue && o.UserID == id); if (dbEmployee != null) { dbEmployee.HasLeft = false; dbEmployee.LeftDate = null; dbEmployee.UpdatedBy = userId; dbEmployee.UpdatedDate = DateTime.Now; } context.SaveChanges(); return(true); } } } catch (Exception ex) { notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Error, Message = ex.Message }; return(false); } }
public bool UpdateAccount(int userId, int id, object dtoItem, out Library.DTO.Notification notification) { DTO.UserData dtoUser = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.UserData>(); notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; try { using (User2MngEntities context = CreateContext()) { UserProfile dbItem = context.UserProfile.FirstOrDefault(o => o.UserId == id); if (dbItem == null) { throw new Exception("User not found!"); } else { dbItem.UpdatedBy = userId; dbItem.UpdatedDate = DateTime.Now; dbItem.UserGroupID = dtoUser.UserGroupID; dbItem.IsActivated = dtoUser.IsActivated; Employee dbEmployee = context.Employee.FirstOrDefault(o => o.UserID == id); if (dbEmployee != null) { dbEmployee.IsSuperUser = dtoUser.IsSuperUser; } context.SaveChanges(); return(true); } } } catch (Exception ex) { notification = new Library.DTO.Notification() { Message = ex.Message, Type = Library.DTO.NotificationType.Error }; return(false); } }
public bool UpdateEmployee(int userId, int id, object dtoItem, out Library.DTO.Notification notification) { DTO.UserProfile dtoProfile = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.UserProfile>(); notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; try { using (User2MngEntities context = CreateContext()) { Employee dbItem = context.Employee.FirstOrDefault(o => o.EmployeeID == id); if (dbItem == null) { throw new Exception("Employee not found!"); } dbItem.UpdatedBy = userId; dbItem.UpdatedDate = DateTime.Now; converter.DTO2DB(dtoProfile, ref dbItem, FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\", userId); dbItem.EmployeeFactory.ToList().ForEach(o => context.EmployeeFactory.Remove(o)); foreach (DTO.EmployeeFactory dtoFactory in dtoProfile.EmployeeFactories) { EmployeeFactory dbFactory = new EmployeeFactory(); dbItem.EmployeeFactory.Add(dbFactory); dbFactory.FactoryID = dtoFactory.FactoryID; } context.SaveChanges(); return(true); } } catch (Exception ex) { notification = new Library.DTO.Notification() { Message = ex.Message, Type = Library.DTO.NotificationType.Error }; return(false); } }
public bool UpdateFactoryAccess(int userId, int id, object dtoItem, out Library.DTO.Notification notification) { List <DTO.FactoryAccess> dtoListFactoryAccess = ((Newtonsoft.Json.Linq.JArray)dtoItem).ToObject <List <DTO.FactoryAccess> >(); notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; try { using (User2MngEntities context = CreateContext()) { UserProfile dbItem = context.UserProfile.FirstOrDefault(o => o.UserId == id); if (dbItem == null) { throw new Exception("User not found!"); } dbItem.UpdatedBy = userId; dbItem.UpdatedDate = DateTime.Now; foreach (DTO.FactoryAccess dtoFactoryAccess in dtoListFactoryAccess) { UserFactoryAccess dbFactoryAccess = dbItem.UserFactoryAccess.FirstOrDefault(o => o.UserFactoryAccessID == dtoFactoryAccess.UserFactoryAccessID); dbFactoryAccess.CanAccess = dtoFactoryAccess.CanAccess; dbFactoryAccess.CanReceiveNotification = dtoFactoryAccess.CanReceiveNotification; } context.SaveChanges(); return(true); } } catch (Exception ex) { notification = new Library.DTO.Notification() { Message = ex.Message, Type = Library.DTO.NotificationType.Error }; return(false); } }
public bool UpdatePermission(int userId, int id, object dtoItem, out Library.DTO.Notification notification) { List <DTO.Permission> dtoListPermission = ((Newtonsoft.Json.Linq.JArray)dtoItem).ToObject <List <DTO.Permission> >(); notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; try { using (User2MngEntities context = CreateContext()) { UserProfile dbItem = context.UserProfile.FirstOrDefault(o => o.UserId == id); if (dbItem == null) { throw new Exception("User not found!"); } dbItem.UpdatedBy = userId; dbItem.UpdatedDate = DateTime.Now; foreach (DTO.Permission dtoPermission in dtoListPermission) { UserPermission dbPermission = dbItem.UserPermission.FirstOrDefault(o => o.UserPermissionID == dtoPermission.UserPermissionID); if (dtoPermission.CanReadEditable) { dbPermission.CanRead = dtoPermission.CanRead; } if (dtoPermission.CanCreateEditable) { dbPermission.CanCreate = dtoPermission.CanCreate; } if (dtoPermission.CanUpdateEditable) { dbPermission.CanUpdate = dtoPermission.CanUpdate; } if (dtoPermission.CanDeleteEditable) { dbPermission.CanDelete = dtoPermission.CanDelete; } if (dtoPermission.CanPrintEditable) { dbPermission.CanPrint = dtoPermission.CanPrint; } if (dtoPermission.CanApproveEditable) { dbPermission.CanApprove = dtoPermission.CanApprove; } if (dtoPermission.CanResetEditable) { dbPermission.CanReset = dtoPermission.CanReset; } } context.SaveChanges(); return(true); } } catch (Exception ex) { notification = new Library.DTO.Notification() { Message = ex.Message, Type = Library.DTO.NotificationType.Error }; return(false); } }
public int InitAccount(int userId, DTO.NewAccountData dtoItem, out Library.DTO.Notification notification) { notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; try { UserProfile dbProfile; Employee dbEmployee; using (User2MngEntities context = CreateContext()) { using (DbContextTransaction scope = context.Database.BeginTransaction()) { context.Database.ExecuteSqlCommand("SELECT * FROM UserProfile WITH (TABLOCKX, HOLDLOCK); SELECT * FROM Employee WITH (TABLOCKX, HOLDLOCK);"); try { dbProfile = new UserProfile(); dbProfile.CreatedBy = userId; dbProfile.CreatedDate = DateTime.Now; dbProfile.AspNetUsersId = dtoItem.AspNetUserId; dbProfile.IsActivated = true; dbProfile.UserGroupID = dtoItem.UserGroupID; context.UserProfile.Add(dbProfile); if (dtoItem.EmployeeID.HasValue) { dbEmployee = context.Employee.FirstOrDefault(o => o.EmployeeID == dtoItem.EmployeeID.Value); } else { dbEmployee = new Employee(); context.Employee.Add(dbEmployee); } context.SaveChanges(); context.User2Mng_function_InitAccount(dbProfile.UserId); dbProfile.UserUD = dbProfile.UserId.ToString(); dbEmployee.EmployeeUD = dbEmployee.EmployeeID.ToString(); dbEmployee.UserID = dbProfile.UserId; dbEmployee.UpdatedBy = userId; dbEmployee.UpdatedDate = DateTime.Now; context.SaveChanges(); } catch (Exception ex) { throw ex; } finally { scope.Commit(); } } return(dbProfile.UserId); } } catch (Exception ex) { notification.Type = Library.DTO.NotificationType.Error; notification.Message = ex.Message; return(-1); } }