You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

314 lines
20 KiB

1 year ago
  1. USE [FastMoneyPro_Remit]
  2. GO
  3. /****** Object: StoredProcedure [dbo].[proc_checkAuthntication] Script Date: 9/27/2019 1:30:14 PM ******/
  4. SET ANSI_NULLS ON
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8. ---EXEC proc_checkAuthntication 'admin','1swift+9','1001'
  9. CREATE procEDURE [dbo].[proc_checkAuthntication]
  10. @userName VARCHAR(100),
  11. @pwd VARCHAR(100),
  12. @agentCode VARCHAR(100),
  13. @errCode INT =NULL OUTPUT
  14. AS
  15. BEGIN TRY
  16. DECLARE @agentId INT,@userId INT,@UserInfoDetail VARCHAR(200),@UserData VARCHAR(200),@ipAddress VARCHAR(20),@attemptsCount INT
  17. SELECT @ipAddress=CAST(CONNECTIONPROPERTY('local_net_address') AS VARCHAR)
  18. SELECT @userId = userId,@agentId=agentId FROM applicationUsers WHERE userName = @userName
  19. SET @UserData ='User:'+ @userName +', AgentCode:'+ CAST(@agentCode AS VARCHAR(20))
  20. SELECT TOP 1
  21. @agentId = agentId
  22. FROM agentMaster WITH(NOLOCK)
  23. WHERE agentId IN (
  24. SELECT agentId FROM applicationUsers WITH(NOLOCK) WHERE userName = @userName
  25. )
  26. IF NOT EXISTS(SELECT 'X' FROM applicationUsers WHERE [userName] = @userName)
  27. BEGIN
  28. --SELECT 1 errorCode, 'Login fails, Incorrect user name or password.' mes, @userName id
  29. SET @UserInfoDetail = 'Reason = Login fails, Incorrect user name.-:::-'+@UserInfoDetail
  30. EXEC proc_applicationLogs
  31. @flag='login',
  32. @logType='Login fails',
  33. @createdBy = @userName,
  34. @Reason='Invalid Username',
  35. @UserData = @UserData,
  36. @fieldValue = @UserInfoDetail,
  37. @agentId=@agentId,
  38. @IP = @ipAddress
  39. SET @errCode= '1'
  40. RETURN
  41. END
  42. IF EXISTS(SELECT 'X' FROM applicationUsers WHERE userName = @userName
  43. AND pwd = dbo.FNAEncryptString(@pwd) AND agentCode = @agentCode
  44. AND ISNULL(isActive, 'N') = 'N'
  45. )
  46. BEGIN
  47. --SELECT 1 errorCode, 'User has not been approved.' mes, @userName id
  48. SET @UserInfoDetail = 'Reason = User has not been approved.-:::-'+@UserInfoDetail
  49. EXEC proc_applicationLogs
  50. @flag='login',
  51. @logType='Login fails',
  52. @createdBy = @userName,
  53. @Reason='User has not been approved',
  54. @UserData = @UserData,
  55. @fieldValue = @UserInfoDetail,
  56. @agentId=@agentId,
  57. @IP = @ipAddress
  58. SET @errCode= '1'
  59. RETURN
  60. END
  61. IF NOT EXISTS(SELECT 'X' FROM applicationUsers WHERE [userName] = @userName
  62. AND ISNULL(isActive, 'N') = 'Y')
  63. BEGIN
  64. --SELECT 1 errorCode, 'Login fails, Incorrect user name or password.' mes, @userName id
  65. SET @UserInfoDetail = 'Reason = Login fails, User is not Active.-:::-'+@UserInfoDetail
  66. EXEC proc_applicationLogs
  67. @flag='login',
  68. @logType='Login fails',
  69. @createdBy = @userName,
  70. @Reason='User is not Active',
  71. @UserData = @UserData,
  72. @fieldValue = @UserInfoDetail,
  73. @agentId=@agentId,
  74. @IP = @ipAddress
  75. SET @errCode= '1'
  76. RETURN
  77. END
  78. SELECT TOP 1 @attemptsCount = loginAttemptCount FROM passwordFormat WITH(NOLOCK)
  79. IF NOT EXISTS(SELECT 'X' FROM applicationUsers WHERE [userName] = @userName
  80. AND pwd = dbo.FNAEncryptString(@pwd) and ISNULL(isActive, 'N') = 'Y')
  81. BEGIN
  82. --SELECT 2 errorCode, 'Login fails, Incorrect user name or password.' mes, @userName id, @attemptsCount ac
  83. SET @UserInfoDetail = 'Reason = Login fails, Incorrect password.-:::-'+@UserInfoDetail
  84. EXEC proc_applicationLogs
  85. @flag='login',
  86. @logType='Login fails',
  87. @createdBy = @userName,
  88. @Reason='Incorrect password',
  89. @UserData = @UserData,
  90. @fieldValue = @UserInfoDetail,
  91. @agentId=@agentId,
  92. @IP = @ipAddress
  93. SET @errCode= '1'
  94. RETURN
  95. END
  96. IF NOT EXISTS(SELECT 'X' FROM applicationUsers WHERE [userName] = @userName
  97. AND pwd = dbo.FNAEncryptString(@pwd) and ISNULL(isActive, 'N') = 'Y'
  98. AND agentCode = @agentCode)
  99. BEGIN
  100. --SELECT 2 errorCode, 'Login fails, Incorrect user name or password.' mes, @userName id, @attemptsCount ac
  101. SET @UserInfoDetail = 'Reason = Login fails, Incorrect AgentCode.-:::-'+@UserInfoDetail
  102. EXEC proc_applicationLogs
  103. @flag='login',
  104. @logType='Login fails',
  105. @createdBy = @userName,
  106. @Reason='Incorrect AgentCode',
  107. @UserData = @UserData,
  108. @fieldValue = @UserInfoDetail,
  109. @agentId=@agentId,
  110. @IP = @ipAddress
  111. SET @errCode= '1'
  112. RETURN
  113. END
  114. IF NOT EXISTS(SELECT 'X' FROM applicationUsers WHERE [userName] = @userName
  115. AND pwd = dbo.FNAEncryptString(@pwd) and ISNULL(isActive, 'N') = 'Y'
  116. AND agentCode = @agentCode --AND employeeId = @employeeId
  117. )
  118. BEGIN
  119. --SELECT 2 errorCode, 'Login fails, Incorrect user name or password.' mes, @userName id, @attemptsCount ac
  120. SET @UserInfoDetail = 'Reason = Login fails, Incorrect userId.-:::-'+@UserInfoDetail
  121. EXEC proc_applicationLogs
  122. @flag='login',
  123. @logType='Login fails',
  124. @createdBy = @userName,
  125. @Reason='Incorrect userId',
  126. @UserData = @UserData,
  127. @fieldValue = @UserInfoDetail,
  128. @agentId=@agentId,
  129. @IP = @ipAddress
  130. SET @errCode= '1'
  131. RETURN
  132. END
  133. IF EXISTS(SELECT 'X' FROM applicationUsers WHERE [userName] = @userName
  134. AND pwd = dbo.FNAEncryptString(@pwd) AND agentCode = @agentCode
  135. --AND employeeId = @employeeId
  136. AND ISNULL(isLocked, 'N') = 'Y')
  137. BEGIN
  138. --SELECT 1 errorCode, 'Your account has been locked. Please, contact your administrator.' mes, @userName id
  139. SET @UserInfoDetail = 'Reason = Your account has been locked. Please, contact your administrator.-:::-'+@UserInfoDetail
  140. EXEC proc_applicationLogs
  141. @flag='login',
  142. @logType='Login fails',
  143. @createdBy = @userName,
  144. @Reason='Your account has been locked',
  145. @UserData = @UserData,
  146. @fieldValue = @UserInfoDetail,
  147. @agentId=@agentId,
  148. @IP = @ipAddress
  149. SET @errCode= '1'
  150. RETURN;
  151. END
  152. IF NOT EXISTS(SELECT 'X' FROM applicationUsers WHERE userName = @userName
  153. AND pwd = dbo.FNAEncryptString(@pwd) AND agentCode = @agentCode
  154. --AND employeeId = @employeeId
  155. AND ISNULL(isDeleted, 'N') <> 'Y'
  156. AND CAST(GETDATE() AS TIME) > loginTime AND CAST(GETDATE() AS TIME) < logoutTime)
  157. BEGIN
  158. --SELECT 1 errorCode, 'You are not permitted to login at this time. Please, contact your administrator' mes, @userName id
  159. SET @UserInfoDetail = 'Reason = You are not permitted to login at this time. Please, contact your administrator-:::-'+@UserInfoDetail
  160. EXEC proc_applicationLogs
  161. @flag='login',
  162. @logType='Login fails',
  163. @createdBy = @userName,
  164. @Reason='Not permitted to login at this time',
  165. @UserData = @UserData,
  166. @fieldValue = @UserInfoDetail,
  167. @agentId=@agentId,
  168. @IP = @ipAddress
  169. SET @errCode= '1'
  170. RETURN
  171. END
  172. IF EXISTS(select top 1 'y' from userLockDetail
  173. where userId =@userId and GETDATE() between startDate
  174. and convert(varchar(20), endDate,101) +' 23:59:59'
  175. and isnull(isDeleted,'N')='N')
  176. BEGIN
  177. --SELECT 1 errorCode, 'You are not permitted to login for this date. Please, contact your administrator' mes, @userName id
  178. SET @UserInfoDetail = 'Reason = You are not permitted to login at this time. Please, contact your administrator-:::-'+@UserInfoDetail
  179. EXEC proc_applicationLogs
  180. @flag='login',
  181. @logType='Login fails',
  182. @createdBy = @userName,
  183. @Reason='Not permitted to login for this date',
  184. @UserData = @UserData,
  185. @fieldValue = @UserInfoDetail,
  186. @agentId=@agentId,
  187. @IP = @ipAddress
  188. SET @errCode= '1'
  189. RETURN
  190. END
  191. -- Last Login date check for Locking
  192. IF EXISTS(select 'X' from applicationUsers
  193. where userId =@userId and
  194. datediff (DAY,lastLoginTs,GETDATE())>=
  195. (select top 1 isnull(lockUserDays,30) from passwordFormat
  196. where isnull(isActive,'N')='Y')
  197. )
  198. BEGIN
  199. update applicationUsers set
  200. isLocked='Y'
  201. ,lastLoginTs=dbo.FNAGetDateInNepalTZ()
  202. where userId = @userId
  203. --SELECT 1 errorCode, 'You are locked this time. Please, contact your administrator' mes, @userName id
  204. SET @UserInfoDetail = 'Reason = You are locked this time. Please, contact your administrator.-:::-'+@UserInfoDetail
  205. EXEC proc_applicationLogs
  206. @flag='login',
  207. @logType='Login fails',
  208. @createdBy = @userName,
  209. @Reason='Not Login for fix period, now user is locked',
  210. @UserData = @UserData,
  211. @fieldValue = @UserInfoDetail,
  212. @agentId=@agentId,
  213. @IP = @ipAddress
  214. INSERT INTO userLockHistory(username, lockReason, createdBy, createdDate)
  215. SELECT @userName, 'Your account has been locked due to not login for fix period', 'system', GETDATE()
  216. SET @errCode= '1'
  217. RETURN;
  218. END
  219. IF EXISTS(SELECT TOP 1 'Y' FROM userLockDetail
  220. WHERE userId =@userId and GETDATE() between startDate
  221. AND CONVERT(VARCHAR(20), endDate,101) +' 23:59:59'
  222. AND ISNULL(isDeleted,'N')='N'
  223. )
  224. BEGIN
  225. --SELECT 1 errorCode, 'You are not permitted to login for this date. Please, contact your administrator' mes, @userName id
  226. SET @UserInfoDetail = 'Reason = You are not permitted to login at this time. Please, contact your administrator-:::-'+@UserInfoDetail
  227. EXEC proc_applicationLogs
  228. @flag='login',
  229. @logType='Login fails',
  230. @createdBy = @userName,
  231. @Reason='Not permitted to login for this date',
  232. @UserData = @UserData,
  233. @fieldValue = @UserInfoDetail,
  234. @agentId=@agentId,
  235. @IP = @ipAddress
  236. SET @errCode= '1'
  237. RETURN;
  238. END
  239. --------------------------------IF SUCCESS
  240. ----UPDATE applicationUsers SET
  241. ---- lastLoginTs = dbo.FNAGetDateInNepalTZ()
  242. ---- WHERE [userName]= @userName
  243. --EXEC proc_applicationUsers @flag = 'userDetail', @userName = @userName
  244. --Audit data starts
  245. EXEC proc_applicationLogs
  246. @flag='login',
  247. @logType='Login',
  248. @createdBy = @userName,
  249. @Reason='Agent Login',
  250. @UserData = @UserData,
  251. @fieldValue = @UserInfoDetail,
  252. @agentId=@agentId,
  253. @IP = @ipAddress
  254. --Audit data ends
  255. SET @errCode= '0'
  256. RETURN
  257. END TRY
  258. BEGIN CATCH
  259. SET @errCode='1'
  260. END CATCH
  261. GO