public static bool Create(Models.Users.UserTokenInfo tokenInfo) { using (var db = new DataAccess.CaraxEntitiy()) { db.UserTokenInfos.Add(tokenInfo); return(db.SaveChanges() > 0); } }
public async static Task <IList <Models.Department> > GetDepartments(Guid token) { var userInfo = AuthenticationLogic.CheckTokenInfo(token); using (var db = new DataAccess.CaraxEntitiy()) { return(await db.Departments.Where(q => q.CompanyCode == userInfo.CompanyCode)?.ToListAsync()); } }
public static bool Update(Guid tokenGuid, DateTime tokenEndDateTime) { using (var db = new DataAccess.CaraxEntitiy()) { var userTokenInfo = db.UserTokenInfos?.FirstOrDefault(q => q.TokenGuid == tokenGuid); if (userTokenInfo == null) { return(false); } userTokenInfo.TokenEndDateTime = tokenEndDateTime; return(db.SaveChanges() > 0); } }
public static List <Models.Users.UserDepartment> GetListByUser(int userId) { using (var db = new DataAccess.CaraxEntitiy()) { return(db.UserDepartments.Where(q => q.UserId == userId)?.Select(q => new UserDepartment { DepartmentCode = q.DepartmentCode, Id = q.Id, Username = q.Username, CompanyCode = q.CompanyCode, UserId = q.UserId }).ToList()); } }
public static async Task <bool> Delete(Guid tokens) { using (var db = new DataAccess.CaraxEntitiy()) { var tokeninfo = db.UserTokenInfos.FirstOrDefault(q => q.TokenGuid == tokens); db.UserTokenInfos.Remove(tokeninfo); db.SaveChanges(); var token = db.Tokens.First(q => q.TokenGuid == tokens); db.Tokens.Remove(token); return(await db.SaveChangesAsync() > 0); } }
public async static Task <bool> Update(Guid token, string departmentCode, Models.Department model) { var userInfo = AuthenticationLogic.CheckTokenInfo(token); using (var db = new DataAccess.CaraxEntitiy()) { model.UpdatedBy = userInfo.Username; model.UpdatedDateTime = DateTime.Now; model.CompanyCode = userInfo.CompanyCode; model.Code = departmentCode; db.Departments.Update(model); return(await db.SaveChangesAsync() > 0); } }
public static UserTokenInfo Get(Guid token) { using (var db = new DataAccess.CaraxEntitiy()) { var singleToken = db.Tokens?.FirstOrDefault(q => q.TokenGuid == token); if (singleToken == null) { return(null); } var user = db.Users.SingleOrDefault(q => q.Username == singleToken.Username && q.DepartmentCode == singleToken.DepartmentCode); return(new Models.Users.UserTokenInfo(tokenGuid: singleToken.TokenGuid, userId: singleToken.UserId, departmentCode: singleToken.DepartmentCode, username: singleToken.Username, level: user.UserLevel, email: user.EMail, createdDateTime: singleToken.CreateDateTime, endDateTime: singleToken.TokenEndDateTime, companyCode: singleToken.CompanyCode)); } }
public static IList <Models.Department> GetAll() { using (var db = new DataAccess.CaraxEntitiy()) { return(db.Departments.Select(q => new Models.Department { Code = q.Code, Address = q.Address, CompanyCode = q.CompanyCode, CreatedBy = q.CreatedBy, CreatedDateTime = q.CreatedDateTime, VisualId = q.VisualId, UpdatedBy = q.UpdatedBy, UpdatedDateTime = q.UpdatedDateTime, Name = q.Name, }).ToList()); } }
public static async Task <Models.Department> Get(string departmentCode) { using (var db = new DataAccess.CaraxEntitiy()) { return(await db.Departments.Where(q => q.Code == departmentCode).Select(q => new Models.Department { Code = q.Code, Address = q.Address, CompanyCode = q.CompanyCode, CreatedBy = q.CreatedBy, CreatedDateTime = q.CreatedDateTime, VisualId = q.VisualId, UpdatedBy = q.UpdatedBy, UpdatedDateTime = q.UpdatedDateTime, Name = q.Name, }).FirstOrDefaultAsync()); } }
public static List <UserTokenInfo> GetTokens(int userId, string userName) { using (var db = new DataAccess.CaraxEntitiy()) { var userTokenInfo = db.UserTokenInfos.Where(q => q.UserId == userId && q.Username == userName).ToList(); var result = new List <UserTokenInfo>(); var user = UserLogic.Get(userId); var departments = DepartmentLogic.GetAll(); var userDepartments = UserDepartmentLogic.GetListByUser(userId); foreach (var department in departments) { var info = userTokenInfo.FirstOrDefault(x => x.DepartmentCode == department.Code); if (info is null) { continue; } if (result.Any(x => x.DepartmentCode == info.DepartmentCode)) { continue; } var userMall = userDepartments.FirstOrDefault(x => x.DepartmentCode == department.Code && x.Username == info.Username); if (userMall is null) { continue; } result.Add(new Models.Users.UserTokenInfo(info.TokenGuid, info.UserId, info.Username, info.DepartmentCode, user.UserLevel, user.EMail, info.CreatedDateTime, info.TokenEndDateTime, info.CompanyCode)); } return(result); } }
private static List <Models.Users.TokenResult> GetUserTokenGuid(string username, string password, string departmentCode) { using (var db = new DataAccess.CaraxEntitiy()) { var userDepartments = db.UserDepartments.Where(q => q.Username == username && q.DepartmentCode == departmentCode).ToList(); if (!userDepartments.Any()) { throw new AuthenticationException(); } var userId = userDepartments.First().UserId; var user = db.Users.FirstOrDefault(q => q.Id == userId); if (!user.IsActive) { throw new AuthenticationException("This user is not active."); } // checkpassword if (!password.GetSHA512(user.PasswordSalt).SequenceEqual(user.PasswordHash)) { throw new AuthenticationException(); } var departments = db.Departments.ToList(); var tokens = new List <TokenResult>(); var dbTokens = UserTokenInfoLogic.GetTokens(userId, username); var dateTime = DateTime.Now; foreach (var info in dbTokens) { info.TokenEndDateTime = dateTime.AddDays(7); Tokens.AddOrUpdate(info.TokenGuid, info); var departmentFirst = departments.FirstOrDefault(x => x.Code == info.DepartmentCode); tokens.Add(new TokenResult { DepartmentCode = info.DepartmentCode, DepartmentName = departmentFirst?.Name, Token = info.TokenGuid }); } Task.Factory.StartNew(() => { foreach (var info in dbTokens) { UserTokenInfoLogic.Update(info.TokenGuid, info.TokenEndDateTime); } }); var userDepartmentWithByUser = UserDepartmentLogic.GetListByUser(userId); if (userDepartmentWithByUser.Count > dbTokens.Count) { foreach (var department in userDepartmentWithByUser.Where(x => dbTokens.All(y => y.DepartmentCode != x.DepartmentCode))) { var departmentFirst = departments.FirstOrDefault(x => x.Code == department.DepartmentCode); // set token var tokenGuid = Guid.NewGuid(); var tokenInfo = new Models.Users.UserTokenInfo(tokenGuid, user.Id, user.Username, department.DepartmentCode, user.UserLevel, user.EMail, dateTime, dateTime.AddDays(7), user.CompanyCode); // add to db UserTokenInfoLogic.Create(tokenInfo); Models.Tokens.Token tk = new Token { CompanyCode = tokenInfo.CompanyCode, CreateDateTime = tokenInfo.CreatedDateTime, DepartmentCode = tokenInfo.DepartmentCode, TokenEndDateTime = tokenInfo.TokenEndDateTime, TokenGuid = tokenInfo.TokenGuid, UserId = tokenInfo.UserId, Username = tokenInfo.Username }; db.Tokens.Add(tk); db.SaveChanges(); // add yo dictionary Tokens.AddOrUpdate(tokenGuid, tokenInfo); tokens.Add(new TokenResult { DepartmentCode = department.DepartmentCode, DepartmentName = departmentFirst?.Name, Token = tokenGuid }); } } return(tokens); } }