public void Save(IssueEmployee data) { var persist = _context.IssueEmployee .Where(x => x.Id == data.Id).FirstOrDefault(); if (persist == null) { var check = _context.IssueEmployee.Where(x => x.Account == data.Account && x.IsDelete == false).Count(); if (check > 0) { throw new ServiceException("아이디가 중복됩니다."); } data.User = new User { Name = data.Name, }; _context.IssueEmployee.Add(data); _context.SaveChanges(); } else { persist.Name = data.Name; persist.Account = data.Account; persist.EmployeeNo = data.EmployeeNo; persist.Tel = data.Tel; persist.Email = data.Email; // _context.SaveChanges(); } }
public void Delete(object id) { try { IssueEmployee data = _context.IssueEmployee.Find(id); data.IsDelete = true; _context.SaveChanges(); } catch (Exception ex) { throw ex; } }
public static IssueEmployee WithoutPassword(this IssueEmployee user) { if (user == null) { return(null); } user.Password = null; return(user); }
public IssueEmployee GetByAccount(string account) { IssueEmployee emp = null; emp = (from e in _context.IssueEmployee.AsNoTracking() where e.IsDelete == false && e.Account == account select e).FirstOrDefault(); if (emp != null) { _context.Entry(emp).State = EntityState.Deleted; } return(emp.WithoutPassword()); }
public IssueEmployee Login(string account, string password, string ip) { IssueEmployee emp = null; try { emp = (from c in _context.IssueEmployee.Include(x => x.User) where c.IsDelete == false && c.Account == account select c).FirstOrDefault(); if (emp != null && emp.Password == password) { // authentication successful so generate jwt token var user = emp.User; if (user == null) { throw new Exception("잘못된 정보입니다."); } var tokenHandler = new JwtSecurityTokenHandler(); var key = Encoding.ASCII.GetBytes(_appSettings.Secret); var tokenDescriptor = new SecurityTokenDescriptor { Subject = new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, user.Id.ToString()), new Claim(ClaimTypes.Role, Role.Admin), new Claim(ClaimTypes.Role, Role.SuperAdmin), new Claim("permissions", "['admin']") }), Expires = DateTime.UtcNow.AddDays(7), SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature) }; var token = tokenHandler.CreateToken(tokenDescriptor); emp.User.Token = tokenHandler.WriteToken(token); _context.SaveChanges(); } else { return(null); } } catch (Exception ex) { throw ex; } _context.Entry(emp).State = EntityState.Deleted; return(emp.WithoutPassword()); }
public IActionResult Create([FromBody] IssueEmployee emp) { if (!User.IsInRole(Role.Admin)) { return(Forbid()); } if (ModelState.IsValid) { try { _service.Save(emp); } catch (ServiceException ex) { return(BadRequest(ex.Message)); } } else { return(BadRequest(ModelState)); } return(Ok(emp)); }
public IActionResult Edit(int id, [FromBody] IssueEmployee emp) { _logger.LogInformation(emp.Dump()); /* * var currentUserId = int.Parse(User.Identity.Name); * if (id != currentUserId) * return Forbid(); * if (!User.IsInRole(Role.Admin)) * return Forbid(); */ if (id != emp.Id) { return(NotFound()); } ModelState.Remove("Password"); // 이 수정에서는 비밀번호 제외 if (ModelState.IsValid) { try { _service.Save(emp); } catch (ServiceException ex) { return(BadRequest(ex.Message)); } } else { foreach (var ms in ModelState.ToArray()) { _logger.LogInformation(ms.Key); } return(BadRequest(ModelState)); } return(Ok(emp)); }