PageRenderTime 99ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/EntLib/CE.Portals.DataService/Services/LoginService.cs

https://bitbucket.org/jeffmccommas/acex
C# | 339 lines | 268 code | 46 blank | 25 comment | 41 complexity | d3316264617824249b69d8832bd27f54 MD5 | raw file
  1. using CE.Portals.DataService.Interfaces;
  2. using System;
  3. using CE.Portals.Integrations.Security;
  4. using System.Linq;
  5. using System.Collections.Generic;
  6. using CE.InsightDataAccess.InsightModels;
  7. using CE.Portals.DataService.Models;
  8. using CE.Portals.Integrations.Common;
  9. using Microsoft.EntityFrameworkCore;
  10. namespace CE.Portals.DataService.Services
  11. {
  12. public class LoginService : ILoginService
  13. {
  14. private readonly InsightsContext _insightsContext;
  15. public LoginService(InsightsContext insightsContext)
  16. {
  17. _insightsContext = insightsContext;
  18. }
  19. //public User GetUsersecretKeys(string clientId, string environment)
  20. //{
  21. // throw new NotImplementedException();
  22. //}
  23. private void UpdateFailedAttemptCount(int userId, int failedLoginAttemptCount)
  24. {
  25. var loginUsers = (from u in _insightsContext.PortalLogin
  26. where u.UserId == userId
  27. select u).SingleOrDefault();
  28. loginUsers.FailedLoginCount = failedLoginAttemptCount;
  29. int maxInvalidLogins = int.Parse(GetClientAppsettings(loginUsers.ClientId.ToString(), ApplicationConstants.MAXINCORRECTLOGINS));
  30. if (failedLoginAttemptCount >= maxInvalidLogins)
  31. {
  32. loginUsers.Enabled = false;
  33. }
  34. //else
  35. //{
  36. // loginUsers.Enabled = loginUsers.Enabled == false ? false : true;
  37. //}
  38. _insightsContext.SaveChanges();
  39. }
  40. public string CheckDbConnection()
  41. {
  42. try
  43. {
  44. var connection = _insightsContext.Database.GetDbConnection();
  45. connection.Open();
  46. connection.Close();
  47. return "Connection Successful";
  48. }
  49. catch (System.Data.SqlClient.SqlException sqlExp)
  50. {
  51. return sqlExp.Message;
  52. }
  53. }
  54. public UserDetails ValidateESPMUser(string userName, string password)
  55. {
  56. string decodedUserName = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(userName));
  57. string decodedPassword = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(password));
  58. var loginUsers = (from u in _insightsContext.PortalLogin
  59. where u.Username == decodedUserName
  60. select new
  61. {
  62. u.Username,
  63. u.PasswordSalt,
  64. u.PasswordHash,
  65. u.UserId,
  66. u.ClientId,
  67. u.FailedLoginCount,
  68. u.Enabled,
  69. u.FirstName,
  70. u.LastName,
  71. u.EnvId
  72. }).FirstOrDefault();
  73. if (loginUsers == null) return null;
  74. var encryptor = new Encryptor(decodedPassword);
  75. if (!encryptor.VerifyHash(decodedPassword, loginUsers.PasswordSalt, loginUsers.PasswordHash))
  76. {
  77. UpdateFailedAttemptCount(loginUsers.UserId, loginUsers.FailedLoginCount + 1);
  78. return new UserDetails()
  79. {
  80. FailedLoginAttempts = loginUsers.FailedLoginCount,
  81. Enabled = loginUsers.Enabled ?? false,
  82. ClientId = loginUsers.ClientId.ToString()
  83. };
  84. }
  85. else if (loginUsers.Enabled == false)
  86. {
  87. return new UserDetails()
  88. {
  89. FailedLoginAttempts = loginUsers.FailedLoginCount,
  90. Enabled = loginUsers.Enabled ?? false,
  91. ClientId = loginUsers.ClientId.ToString()
  92. };
  93. }
  94. else
  95. {
  96. UpdateFailedAttemptCount(loginUsers.UserId, 0);
  97. return new UserDetails()
  98. {
  99. ClientId = loginUsers.ClientId.ToString(),
  100. UserId = loginUsers.UserId.ToString(),
  101. FailedLoginAttempts = 0,
  102. UserName = loginUsers.Username,
  103. FirstName = loginUsers.FirstName,
  104. LastName = loginUsers.LastName
  105. };
  106. }
  107. }
  108. public UserDetails GetInsightsUserCreds(int clientId, string apiUserType, int envId)
  109. {
  110. var loginUsers = (from ue in _insightsContext.UserEnvironment
  111. join u in _insightsContext.User on ue.UserId equals u.UserId
  112. where u.ClientId == clientId && u.ActorName == apiUserType && ue.EnvId == envId
  113. select new { ue.CesecretAccessKey, u.CeaccessKeyId }).FirstOrDefault();
  114. if (loginUsers == null) return null;
  115. else
  116. return new UserDetails() { ClientId = clientId.ToString(), CeAccessKeyId = loginUsers.CeaccessKeyId.ToString(), CeSecretAccessKey = loginUsers.CesecretAccessKey.ToString() };
  117. }
  118. //public List<UserPortalList> GetAllPortals(string userId, string clientId)
  119. //{
  120. // var portalList = (from p in _insightsContext.Portals
  121. // join up in _insightsContext.UserPortal on p.PortalId equals up.PortalId
  122. // join u in _insightsContext.PortalLogin on up.UserId equals u.UserId
  123. // where up.UserId == Int32.Parse(userId)
  124. // where u.ClientId == Int32.Parse(clientId)
  125. // where u.Enabled == true
  126. // select new { p.PortalId, p.PortalName, p.PortalComponent, up.IsDefault });
  127. // var userPortalList = new List<UserPortalList>();
  128. // foreach (var portal in portalList)
  129. // {
  130. // userPortalList.Add(new UserPortalList() { PortalId = portal.PortalId, PortalName = portal.PortalName, IsDefault = portal.IsDefault, PortalComponent = portal.PortalComponent });
  131. // }
  132. // //userPortalList.Add(new UserPortalList() { IsDefault = true, PortalComponent = "BenchMark", PortalName = "Energy Star", PortalId = 1 });
  133. // return userPortalList;
  134. //}
  135. public string GetClientESPMSecretKeys(string clientId)
  136. {
  137. var secretKey = (from cpc in _insightsContext.ClientPortalConfig
  138. join pc in _insightsContext.PortalConfig on cpc.PortalConfigId equals pc.PortalConfigId
  139. where pc.IsEnabled == true
  140. where cpc.IsEnabled == true
  141. where pc.PortalConfigName == ApplicationConstants.ESPMSecterKey
  142. where cpc.ClientId == Int32.Parse(clientId)
  143. select new { cpc.PortalConfigValue });
  144. if (secretKey.Any())
  145. {
  146. return secretKey.FirstOrDefault().PortalConfigValue;
  147. }
  148. else
  149. { return null; }
  150. }
  151. public string GetClientAppsettings(string clientId, string appSettingKey)
  152. {
  153. var secretKey = (from cpc in _insightsContext.ClientPortalConfig
  154. join pc in _insightsContext.PortalConfig on cpc.PortalConfigId equals pc.PortalConfigId
  155. where pc.IsEnabled == true
  156. where cpc.IsEnabled == true
  157. where pc.PortalConfigName == appSettingKey
  158. where cpc.ClientId == Int32.Parse(clientId)
  159. select new { cpc.PortalConfigValue });
  160. if (secretKey.Any())
  161. {
  162. return secretKey.FirstOrDefault().PortalConfigValue;
  163. }
  164. else
  165. { return null; }
  166. }
  167. public UserDetails GetUserDetails(int userId)
  168. {
  169. var userDetails = (from p in _insightsContext.PortalLogin
  170. join c in _insightsContext.Client on p.ClientId equals c.ClientId
  171. where p.UserId == userId
  172. select new
  173. {
  174. p.FirstName,
  175. p.LastName,
  176. p.UserType,
  177. p.Enabled,
  178. c.Description,
  179. c.Name,
  180. p.Username,
  181. p.FailedLoginCount,
  182. p.UserId,
  183. p.ClientId
  184. }).FirstOrDefault();
  185. return new UserDetails()
  186. {
  187. FirstName = userDetails.FirstName,
  188. LastName = userDetails.LastName,
  189. IsAdminUser = (userDetails.UserType == "admin"),
  190. Enabled = userDetails.Enabled ?? false,
  191. ClientDescription = userDetails.Description,
  192. ClientName = userDetails.Name,
  193. FailedLoginAttempts = userDetails.FailedLoginCount,
  194. UserName = userDetails.Username,
  195. UserId = userDetails.UserId.ToString(),
  196. ClientId = userDetails.ClientId.ToString()
  197. };
  198. }
  199. public void AddNewUser(UserDetails userDetails, int enVironmentId)
  200. {
  201. var encryptor = new Encryptor(userDetails.PassWord);
  202. var salt = encryptor.GenerateSalt();
  203. PortalLogin portalLogin = new PortalLogin()
  204. {
  205. ClientId = int.Parse(userDetails.ClientId),
  206. Username = userDetails.UserName,
  207. FirstName = userDetails.FirstName,
  208. LastName = userDetails.LastName,
  209. PasswordSalt = salt,
  210. PasswordHash = encryptor.GenerateHashWithSalt(userDetails.PassWord, salt),
  211. UpdateDate = DateTime.UtcNow,
  212. EnvId = Byte.Parse(enVironmentId.ToString()),
  213. UserType = userDetails.IsAdminUser == true ? "admin" : "std",
  214. Enabled = userDetails.Enabled,
  215. FailedLoginCount = 0
  216. };
  217. _insightsContext.PortalLogin.Add(portalLogin);
  218. _insightsContext.SaveChanges();
  219. }
  220. public Boolean CheckIfUserExists(string userName)
  221. {
  222. var loggedinuserDetails = (from p in _insightsContext.PortalLogin
  223. where p.Username == userName
  224. select new { p }).FirstOrDefault();
  225. return loggedinuserDetails != null;
  226. }
  227. public void UpdateUser(UserDetails userDetails, Boolean status)
  228. {
  229. var loginUsers = (from u in _insightsContext.PortalLogin
  230. where u.UserId == int.Parse(userDetails.UserId)
  231. select u).SingleOrDefault();
  232. if (!status)
  233. {
  234. loginUsers.FirstName = userDetails.FirstName;
  235. loginUsers.LastName = userDetails.LastName;
  236. loginUsers.Username = userDetails.UserName;
  237. if (userDetails.PassWord != null)
  238. {
  239. var salt = new Encryptor(userDetails.PassWord).GenerateSalt();
  240. loginUsers.PasswordSalt = salt;
  241. loginUsers.PasswordHash = new Encryptor(userDetails.PassWord).GenerateHashWithSalt(userDetails.PassWord, salt);
  242. }
  243. loginUsers.UpdateDate = DateTime.UtcNow;
  244. loginUsers.UserType = userDetails.IsAdminUser == true ? "admin" : "std";
  245. loginUsers.Enabled = userDetails.Enabled;
  246. loginUsers.FailedLoginCount = userDetails.FailedLoginAttempts;
  247. }
  248. else
  249. {
  250. loginUsers.Enabled = userDetails.Enabled;
  251. loginUsers.FailedLoginCount = userDetails.FailedLoginAttempts;
  252. }
  253. _insightsContext.PortalLogin.Update(loginUsers);
  254. _insightsContext.SaveChanges();
  255. }
  256. public UserDetails GetUserProfile(int userId)
  257. {
  258. var userDetails = (from p in _insightsContext.PortalLogin
  259. join c in _insightsContext.Client on p.ClientId equals c.ClientId
  260. where p.UserId == userId
  261. select new { p.FirstName, p.LastName, p.UserType, p.Username, p.Enabled, c.Description, c.Name, p.FailedLoginCount }).FirstOrDefault();
  262. return new UserDetails()
  263. {
  264. FirstName = userDetails.FirstName,
  265. LastName = userDetails.LastName,
  266. IsAdminUser = (userDetails.UserType == "admin"),
  267. Enabled = userDetails.Enabled ?? false,
  268. ClientDescription = userDetails.Description,
  269. ClientName = userDetails.Name,
  270. FailedLoginAttempts = userDetails.FailedLoginCount,
  271. UserName = userDetails.Username
  272. };
  273. }
  274. public List<UserDetails> GetUserList(int clientId)
  275. {
  276. List<UserDetails> userDetails = new List<UserDetails>();
  277. var users = from p in _insightsContext.PortalLogin where p.UserType != "Web"
  278. where p.ClientId == clientId select new { p.FirstName, p.LastName, p.UserType, p.UserId };
  279. foreach (var user in users)
  280. {
  281. userDetails.Add(new UserDetails()
  282. {
  283. FirstName = user.FirstName,
  284. LastName = user.LastName,
  285. IsAdminUser = user.UserType == "admin" ? true : false,
  286. DisplayName = user.LastName + "," + user.FirstName,
  287. UserId = user.UserId.ToString()
  288. });
  289. }
  290. return userDetails;
  291. }
  292. }
  293. }