|
|
|
@ -25,7 +25,7 @@ namespace IoTCenter.Api.Controllers
|
|
|
|
|
{
|
|
|
|
|
private readonly IWebHostEnvironment _env;
|
|
|
|
|
private readonly IHttpClientFactory _httpClientFactory;
|
|
|
|
|
private readonly ILogger<ProjectController> logger;
|
|
|
|
|
private readonly ILogger<ProjectController> _logger;
|
|
|
|
|
private readonly IConfiguration _cfg;
|
|
|
|
|
private readonly ISettingService _settingService;
|
|
|
|
|
private readonly IRepository<Organ> _organRepo;
|
|
|
|
@ -48,7 +48,7 @@ namespace IoTCenter.Api.Controllers
|
|
|
|
|
{
|
|
|
|
|
this._env = env;
|
|
|
|
|
this._httpClientFactory = httpClientFactory;
|
|
|
|
|
this.logger = logger;
|
|
|
|
|
this._logger = logger;
|
|
|
|
|
this._cfg = cfg;
|
|
|
|
|
this._settingService = settingService;
|
|
|
|
|
this._organRepo = organRepo;
|
|
|
|
@ -60,77 +60,136 @@ namespace IoTCenter.Api.Controllers
|
|
|
|
|
|
|
|
|
|
[AllowAnonymous]
|
|
|
|
|
[HttpGet]
|
|
|
|
|
[Route("/Cas")]
|
|
|
|
|
public IActionResult Cas(string ticket)
|
|
|
|
|
public IActionResult CasLogin(string ticket, string home)
|
|
|
|
|
{
|
|
|
|
|
var home = this._settingService.GetSetting("home")?.Value ?? "http://10.255.253.183";
|
|
|
|
|
var sso = this._settingService.GetSetting("sso")?.Value ?? "http://10.255.253.190/dsssoserver";
|
|
|
|
|
if (string.IsNullOrEmpty(ticket))
|
|
|
|
|
{//跳转到cas
|
|
|
|
|
var url = sso + "/login?service=" + HttpUtility.UrlEncode(home);
|
|
|
|
|
return Redirect(url);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var url = sso + "/serviceValidate?ticket=" + ticket + "&service=" + HttpUtility.UrlEncode(home);
|
|
|
|
|
var httpClient = this._httpClientFactory.CreateClient();
|
|
|
|
|
var result = httpClient.GetAsync(url).Result.Content.ReadAsStringAsync().Result;
|
|
|
|
|
var userName = this.NeiMengWuHai(result);
|
|
|
|
|
this.UpdateInternal(userName);
|
|
|
|
|
home += "?accessToken=" + Request.HttpContext.GetToken(userName, _cfg, DateTime.Now.AddYears(100));
|
|
|
|
|
home += "&refreshToken=" + Request.HttpContext.GetToken(userName, _cfg, DateTime.Now.AddYears(100));
|
|
|
|
|
return Redirect(home);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[AllowAnonymous]
|
|
|
|
|
[HttpGet]
|
|
|
|
|
public IActionResult CasLogin(string ticket, string home)
|
|
|
|
|
{
|
|
|
|
|
var sso = this._settingService.GetSetting("sso")?.Value;
|
|
|
|
|
if (_env.IsDevelopment())
|
|
|
|
|
{
|
|
|
|
|
sso = sso ?? "http://kpsso.kpedu.com";//debug
|
|
|
|
|
home = "http://221.194.113.154:8100";//debug
|
|
|
|
|
}
|
|
|
|
|
var url = sso + "/serviceValidate?ticket=" + ticket + "&service=" + HttpUtility.UrlEncode(home);
|
|
|
|
|
var httpClient = this._httpClientFactory.CreateClient();
|
|
|
|
|
var result = httpClient.GetAsync(url).Result.Content.ReadAsStringAsync().Result;
|
|
|
|
|
//string userName = TangShanKaiPing(result);
|
|
|
|
|
var userName = this.NeiMengWuHai(result);
|
|
|
|
|
if (_env.IsDevelopment())
|
|
|
|
|
{
|
|
|
|
|
home = "http://localhost:8100";//debug
|
|
|
|
|
}
|
|
|
|
|
home += "?accessToken=" + Request.HttpContext.GetToken(userName, _cfg, DateTime.Now.AddYears(100));
|
|
|
|
|
home += "&refreshToken=" + Request.HttpContext.GetToken(userName, _cfg, DateTime.Now.AddYears(100));
|
|
|
|
|
return Redirect(home);
|
|
|
|
|
}
|
|
|
|
|
//[AllowAnonymous]
|
|
|
|
|
//[HttpGet]
|
|
|
|
|
//public IActionResult CasLogin(string ticket, string home)
|
|
|
|
|
//{
|
|
|
|
|
// var sso = this._settingService.GetSetting("sso")?.Value;
|
|
|
|
|
// if (_env.IsDevelopment())
|
|
|
|
|
// {
|
|
|
|
|
// sso = sso ?? "http://kpsso.kpedu.com";//debug
|
|
|
|
|
// home = "http://221.194.113.154:8100";//debug
|
|
|
|
|
// }
|
|
|
|
|
// var url = sso + "/serviceValidate?ticket=" + ticket + "&service=" + HttpUtility.UrlEncode(home);
|
|
|
|
|
// var httpClient = this._httpClientFactory.CreateClient();
|
|
|
|
|
// var result = httpClient.GetAsync(url).Result.Content.ReadAsStringAsync().Result;
|
|
|
|
|
// //string userName = TangShanKaiPing(result);
|
|
|
|
|
// var userName = this.NeiMengWuHai(result);
|
|
|
|
|
// if (_env.IsDevelopment())
|
|
|
|
|
// {
|
|
|
|
|
// home = "http://localhost:8100";//debug
|
|
|
|
|
// }
|
|
|
|
|
// home += "?accessToken=" + Request.HttpContext.GetToken(userName, _cfg, DateTime.Now.AddYears(100));
|
|
|
|
|
// home += "&refreshToken=" + Request.HttpContext.GetToken(userName, _cfg, DateTime.Now.AddYears(100));
|
|
|
|
|
// return Redirect(home);
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
//[AllowAnonymous]
|
|
|
|
|
//[HttpGet]
|
|
|
|
|
//public IActionResult Update(string userName, string realName, string organName, string organNumber, long timestamp, string role, string token)
|
|
|
|
|
//{
|
|
|
|
|
// if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(organName))
|
|
|
|
|
// {
|
|
|
|
|
// var message = "参数不能为空";
|
|
|
|
|
// this.logger.LogError(message);
|
|
|
|
|
// return Problem(message);
|
|
|
|
|
// }
|
|
|
|
|
// try
|
|
|
|
|
// {
|
|
|
|
|
// var query = this.Request.QueryString.ToString().RemoveParam("token").TrimStart('?').Trim();
|
|
|
|
|
// var token2 = $"{query},123456".Md5();
|
|
|
|
|
// if (token2 != token)
|
|
|
|
|
// {
|
|
|
|
|
// var message = "token无效";
|
|
|
|
|
// this.logger.LogError(message);
|
|
|
|
|
// throw new Exception(message);
|
|
|
|
|
// }
|
|
|
|
|
// var sendTime = DateTimeOffset.FromUnixTimeSeconds(timestamp);
|
|
|
|
|
// var seconds = (DateTime.UtcNow - sendTime).TotalSeconds;
|
|
|
|
|
// if (seconds > 60)
|
|
|
|
|
// {
|
|
|
|
|
// var message = $"timestamp差距{seconds}秒";
|
|
|
|
|
// this.logger.LogError(message);
|
|
|
|
|
// throw new Exception(message);
|
|
|
|
|
// }
|
|
|
|
|
// User user = UpdateOrganUser(userName, realName, organName, organNumber);
|
|
|
|
|
// var roles = role.Split(',');
|
|
|
|
|
// var userRoles = this._userRoleRepo.Table().Where(o => o.User.UserName == userName);
|
|
|
|
|
// if (roles.Any())
|
|
|
|
|
// {
|
|
|
|
|
// foreach (var item in roles)
|
|
|
|
|
// {
|
|
|
|
|
// var role2 = _roleRepo.Table().FirstOrDefault(o => o.Name == item);
|
|
|
|
|
// if (role2 == null)
|
|
|
|
|
// {
|
|
|
|
|
// role2 = new Role { Name = item };
|
|
|
|
|
// _roleRepo.Add(role2);
|
|
|
|
|
// _roleRepo.SaveChanges();
|
|
|
|
|
// }
|
|
|
|
|
// if (!userRoles.Any(o => o.Role.Name == item))
|
|
|
|
|
// {
|
|
|
|
|
// _userRoleRepo.Add(new UserRole { UserId = user.Id, RoleId = role2.Id });
|
|
|
|
|
// _userRoleRepo.SaveChanges();
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// catch (Exception ex)
|
|
|
|
|
// {
|
|
|
|
|
// this.logger.LogError(ex.ToString());
|
|
|
|
|
// return Problem(ex.Message);
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// return Ok(new
|
|
|
|
|
// {
|
|
|
|
|
// AccessToken = Request.HttpContext.GetToken(userName, _cfg, DateTime.Now.AddYears(100)),
|
|
|
|
|
// RefreshToken = Request.HttpContext.GetToken(userName, _cfg, DateTime.Now.AddYears(100)),
|
|
|
|
|
// });
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
private string NeiMengWuHai(string result)
|
|
|
|
|
{
|
|
|
|
|
this.logger.LogInformation(result);
|
|
|
|
|
//d_wudaqu 这个是user,300348这个是区id,district_id。15785这个是person_id。63,12,335这个是rold_id
|
|
|
|
|
//d_wudaqu 300348 5LmM6L6+5Yy6566h55CG5ZGY 15785 d_wudaqu 5LmM6L6+5Yy65pWZ6IKy5bGA 5 63,12,335
|
|
|
|
|
this._logger.LogInformation(result);
|
|
|
|
|
var doc = new XmlDocument();
|
|
|
|
|
doc.LoadXml(result);
|
|
|
|
|
var userName = doc.GetElementsByTagName("cas:USER_NAME")[0].InnerText.Trim();
|
|
|
|
|
var personId = Convert.ToInt32(doc.GetElementsByTagName("cas:PERSON_ID")[0].InnerText.Trim());
|
|
|
|
|
this.UpdateInternal(userName);
|
|
|
|
|
return userName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void UpdateInternal(string userName)
|
|
|
|
|
{
|
|
|
|
|
var connStr = this._cfg.GetConnectionString("mariyadb");
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using var db = new BaseDbContext(connStr);
|
|
|
|
|
var personId = db.Set<PersonLogin>().Where(o => o.LoginName == userName).Select(o=>o.PersonId).FirstOrDefault();
|
|
|
|
|
var user = db.Set<Person>().FirstOrDefault(o => o.Id == personId);
|
|
|
|
|
this._logger.LogInformation(user.ToJson());
|
|
|
|
|
var organ = db.Set<Organization>().FirstOrDefault(o => o.OrganId == user.BureauId);
|
|
|
|
|
var roles = (from perssonRole in db.Set<PersonRole>().Where(o => o.PersonId == user.Id)
|
|
|
|
|
this._logger.LogInformation(organ.ToJson());
|
|
|
|
|
var roles = (from perssonRole in db.Set<PersonRole>().Where(o => o.PersonId == personId)
|
|
|
|
|
join role in db.Set<SystemRole>() on perssonRole.RoleId equals role.Id
|
|
|
|
|
select role.RoleName).Distinct().ToList();
|
|
|
|
|
this.Update(userName, user.RealName, organ.Name, organ.Id.ToString(), roles);
|
|
|
|
|
this.Update(userName,user.Email, user.RealName, organ.Name, organ.Id.ToString(), roles);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
return connStr + ex.ToString();
|
|
|
|
|
this._logger.LogError( connStr + ex.ToString());
|
|
|
|
|
}
|
|
|
|
|
return userName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string TangShanKaiPing(string result)
|
|
|
|
@ -145,9 +204,10 @@ namespace IoTCenter.Api.Controllers
|
|
|
|
|
return userName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Update(string userName, string realName, string organName, string organNumber, List<string> roles)
|
|
|
|
|
private void Update(string userName,string email, string realName, string organName, string organNumber, List<string> roles)
|
|
|
|
|
{
|
|
|
|
|
var organ = this._organRepo.Table().FirstOrDefault(o => o.Name == organName);
|
|
|
|
|
//更新机构
|
|
|
|
|
var organ = this._organRepo.Table().FirstOrDefault(o => o.Number == organNumber);
|
|
|
|
|
if (organ == null)
|
|
|
|
|
{
|
|
|
|
|
organ = new Organ { Name = organName, Number = organNumber };
|
|
|
|
@ -155,29 +215,44 @@ namespace IoTCenter.Api.Controllers
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
organ.Number = organNumber;
|
|
|
|
|
organ.Name = organName;
|
|
|
|
|
}
|
|
|
|
|
this._organRepo.SaveChanges();
|
|
|
|
|
|
|
|
|
|
//更新用户
|
|
|
|
|
var user = this._userRepo.Table().FirstOrDefault(o => o.UserName == userName);
|
|
|
|
|
if (user == null)
|
|
|
|
|
{
|
|
|
|
|
user = new User
|
|
|
|
|
{
|
|
|
|
|
UserName = userName,
|
|
|
|
|
RealName=realName,
|
|
|
|
|
NickName = realName,
|
|
|
|
|
Email = $"{userName}@test.com"
|
|
|
|
|
Email = string.IsNullOrEmpty(email)? $"{userName}@test.com":email
|
|
|
|
|
};
|
|
|
|
|
this._userRepo.Add(user);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
user.RealName = realName;
|
|
|
|
|
user.NickName = realName;
|
|
|
|
|
user.Email = string.IsNullOrEmpty(email) ? $"{userName}@test.com" : email;
|
|
|
|
|
}
|
|
|
|
|
this._userRepo.SaveChanges();
|
|
|
|
|
var organUser = this._organUserRepo.Table().FirstOrDefault(o => o.UserId == user.Id && o.Organ.Id == organ.Id);
|
|
|
|
|
|
|
|
|
|
//添加用户到机构
|
|
|
|
|
var organUser = this._organUserRepo.Table().FirstOrDefault(o => o.UserId == user.Id);
|
|
|
|
|
if (organUser == null)
|
|
|
|
|
{
|
|
|
|
|
organUser = new OrganUser { UserId = user.Id, OrganId = organ.Id };
|
|
|
|
|
this._organUserRepo.SaveChanges();
|
|
|
|
|
this._organUserRepo.Add(organUser);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
organUser.OrganId = organ.Id;
|
|
|
|
|
}
|
|
|
|
|
this._organUserRepo.SaveChanges();
|
|
|
|
|
|
|
|
|
|
//更新角色
|
|
|
|
|
if (roles.Any())
|
|
|
|
|
{
|
|
|
|
|
foreach (var item in roles)
|
|
|
|
@ -206,7 +281,7 @@ namespace IoTCenter.Api.Controllers
|
|
|
|
|
if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(organName))
|
|
|
|
|
{
|
|
|
|
|
var message = "参数不能为空";
|
|
|
|
|
this.logger.LogError(message);
|
|
|
|
|
this._logger.LogError(message);
|
|
|
|
|
return Problem(message);
|
|
|
|
|
}
|
|
|
|
|
try
|
|
|
|
@ -216,7 +291,7 @@ namespace IoTCenter.Api.Controllers
|
|
|
|
|
if (token2 != token)
|
|
|
|
|
{
|
|
|
|
|
var message = "token无效";
|
|
|
|
|
this.logger.LogError(message);
|
|
|
|
|
this._logger.LogError(message);
|
|
|
|
|
throw new Exception(message);
|
|
|
|
|
}
|
|
|
|
|
var sendTime = DateTimeOffset.FromUnixTimeSeconds(timestamp);
|
|
|
|
@ -224,34 +299,14 @@ namespace IoTCenter.Api.Controllers
|
|
|
|
|
if (seconds > 60)
|
|
|
|
|
{
|
|
|
|
|
var message = $"timestamp差距{seconds}秒";
|
|
|
|
|
this.logger.LogError(message);
|
|
|
|
|
this._logger.LogError(message);
|
|
|
|
|
throw new Exception(message);
|
|
|
|
|
}
|
|
|
|
|
User user = UpdateOrganUser(userName, realName, organName, organNumber);
|
|
|
|
|
var roles = role.Split(',');
|
|
|
|
|
var userRoles = this._userRoleRepo.Table().Where(o => o.User.UserName == userName);
|
|
|
|
|
if (roles.Any())
|
|
|
|
|
{
|
|
|
|
|
foreach (var item in roles)
|
|
|
|
|
{
|
|
|
|
|
var role2 = _roleRepo.Table().FirstOrDefault(o => o.Name == item);
|
|
|
|
|
if (role2 == null)
|
|
|
|
|
{
|
|
|
|
|
role2 = new Role { Name = item };
|
|
|
|
|
_roleRepo.Add(role2);
|
|
|
|
|
_roleRepo.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
if (!userRoles.Any(o => o.Role.Name == item))
|
|
|
|
|
{
|
|
|
|
|
_userRoleRepo.Add(new UserRole { UserId = user.Id, RoleId = role2.Id });
|
|
|
|
|
_userRoleRepo.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
this.UpdateInternal(userName);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
this.logger.LogError(ex.ToString());
|
|
|
|
|
this._logger.LogError(ex.ToString());
|
|
|
|
|
return Problem(ex.Message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|