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.

286 lines
17 KiB

1 year ago
  1. USE [FastMoneyPro_Remit]
  2. GO
  3. /****** Object: StoredProcedure [dbo].[ws_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. CREATE PROCEDURE [dbo].[ws_proc_checkAuthntication]
  9. @userName VARCHAR(100),
  10. @pwd VARCHAR(100),
  11. @agentCode VARCHAR(100),
  12. @errCode INT = NULL OUTPUT,
  13. @autMsg VARCHAR(500) = NULL OUTPUT
  14. AS
  15. SET NOCOUNT ON
  16. SET XACT_ABORT ON
  17. BEGIN TRY
  18. SELECT @userName = ISNULL(@userName, ''), @pwd = ISNULL(@pwd, ''), @agentCode = ISNULL(@agentCode, '')
  19. DECLARE @agentId INT, @userId INT, @UserInfoDetail VARCHAR(200), @UserData VARCHAR(200)
  20. ,@ipAddress VARCHAR(20),@attemptsCount INT, @isAPIUser VARCHAR(10), @GMTDate varchar(200)
  21. ,@userPwd VARCHAR(50), @userAgentCode VARCHAR(50), @isActive CHAR(1), @isLocked CHAR(1), @loginTime TIME, @logoutTime TIME
  22. ,@lastLoginTs DATETIME, @isAgentActive CHAR(1)
  23. SET @GMTDate = GETDATE()
  24. SELECT @ipAddress = CAST(CONNECTIONPROPERTY('local_net_address') AS VARCHAR)
  25. SELECT
  26. @userId = userId
  27. ,@agentId = au.agentId
  28. ,@isAPIUser = accessMode
  29. ,@userPwd = pwd
  30. ,@userAgentCode = au.agentCode
  31. ,@isActive = ISNULL(au.isActive, 'N')
  32. ,@isLocked = ISNULL(isLocked, 'N')
  33. ,@loginTime = loginTime
  34. ,@logoutTime = logoutTime
  35. ,@lastLoginTs = lastLoginTs
  36. ,@isAgentActive = ISNULL(am.isActive, 'N')
  37. FROM applicationUsers au WITH(NOLOCK)
  38. INNER JOIN agentMaster am WITH(NOLOCK) ON au.agentId = am.agentId and am.agentType = 2903
  39. WHERE userName = @userName AND ISNULL(au.isDeleted, 'N') = 'N'
  40. --AND AU.isActive = 'Y' AND am.isActive = 'Y'
  41. SET @UserData = 'User:' + @userName + ', AgentCode:' + CAST(@agentCode AS VARCHAR(20))
  42. IF isnull(@isAPIUser,'') <> 'WS'
  43. BEGIN
  44. SET @UserInfoDetail = 'Reason = you do not have access.-:::-' + @UserInfoDetail
  45. EXEC proc_applicationLogs
  46. @flag = 'login',
  47. @logType = 'Login fails',
  48. @createdBy = @userName,
  49. @Reason = 'User not allowed to access Web Service',
  50. @UserData = @UserData,
  51. @fieldValue = @UserInfoDetail,
  52. @agentId = @agentId,
  53. @IP = @ipAddress
  54. SET @errCode = '1'
  55. SET @autMsg = 'You do not have access to login.'
  56. RETURN
  57. END
  58. IF @userId IS NULL
  59. BEGIN
  60. SET @UserInfoDetail = 'Reason = Login fails, Incorrect user name.-:::-' + @UserInfoDetail
  61. EXEC proc_applicationLogs
  62. @flag = 'login',
  63. @logType = 'Login fails',
  64. @createdBy = @userName,
  65. @Reason = 'Invalid Username',
  66. @UserData = @UserData,
  67. @fieldValue = @UserInfoDetail,
  68. @agentId = @agentId,
  69. @IP = @ipAddress
  70. SET @errCode = '1'
  71. SET @autMsg = 'Login fails, Authentication Fail.'
  72. RETURN
  73. END
  74. IF @isActive = 'N'
  75. BEGIN
  76. SET @UserInfoDetail = 'Reason = User is not active.-:::-'+@UserInfoDetail
  77. EXEC proc_applicationLogs
  78. @flag = 'login',
  79. @logType = 'Login fails',
  80. @createdBy = @userName,
  81. @Reason = 'User is not active',
  82. @UserData = @UserData,
  83. @fieldValue = @UserInfoDetail,
  84. @agentId = @agentId,
  85. @IP = @ipAddress
  86. SET @errCode = '1'
  87. SET @autMsg = 'User is not active'
  88. RETURN
  89. END
  90. IF @isAgentActive = 'N'
  91. BEGIN
  92. SET @UserInfoDetail = 'Reason = Agent is not active.-:::-' + @UserInfoDetail
  93. EXEC proc_applicationLogs
  94. @flag = 'login',
  95. @logType = 'Login fails',
  96. @createdBy = @userName,
  97. @Reason = 'Agent is not active',
  98. @UserData = @UserData,
  99. @fieldValue = @UserInfoDetail,
  100. @agentId = @agentId,
  101. @IP = @ipAddress
  102. SET @errCode = '1'
  103. SET @autMsg = 'Agent is not active'
  104. RETURN
  105. END
  106. SELECT TOP 1 @attemptsCount = loginAttemptCount FROM passwordFormat WITH(NOLOCK)
  107. IF @userPwd <> dbo.FNAEncryptString(@pwd)
  108. BEGIN
  109. SET @UserInfoDetail = 'Reason = Login fails, Incorrect password.-:::-' + @UserInfoDetail
  110. EXEC proc_applicationLogs
  111. @flag = 'login',
  112. @logType = 'Login fails',
  113. @createdBy = @userName,
  114. @Reason = 'Incorrect password',
  115. @UserData = @UserData,
  116. @fieldValue = @UserInfoDetail,
  117. @agentId = @agentId,
  118. @IP = @ipAddress
  119. SET @errCode = '1'
  120. SET @autMsg = 'Login fails, Authentication Fail.'
  121. RETURN
  122. END
  123. IF @userAgentCode <> @agentCode
  124. BEGIN
  125. SET @UserInfoDetail = 'Reason = Login fails, Incorrect AgentCode.-:::-' + @UserInfoDetail
  126. EXEC proc_applicationLogs
  127. @flag = 'login',
  128. @logType = 'Login fails',
  129. @createdBy = @userName,
  130. @Reason = 'Incorrect AgentCode',
  131. @UserData = @UserData,
  132. @fieldValue = @UserInfoDetail,
  133. @agentId = @agentId,
  134. @IP = @ipAddress
  135. SET @errCode = '1'
  136. SET @autMsg = 'Login fails, Authentication Fail.'
  137. RETURN
  138. END
  139. IF @isLocked = 'Y'
  140. BEGIN
  141. SET @UserInfoDetail = 'Reason = Your account has been locked. Please, contact your administrator.-:::-' + @UserInfoDetail
  142. EXEC proc_applicationLogs
  143. @flag = 'login',
  144. @logType = 'Login fails',
  145. @createdBy = @userName,
  146. @Reason = 'Your account has been locked',
  147. @UserData = @UserData,
  148. @fieldValue = @UserInfoDetail,
  149. @agentId = @agentId,
  150. @IP = @ipAddress
  151. SET @errCode = '1'
  152. SET @autMsg = 'Your account has been locked. Please, contact your administrator.'
  153. RETURN;
  154. END
  155. IF CAST(GETDATE() AS TIME) < @loginTime AND CAST(GETDATE() AS TIME) > @logoutTime
  156. BEGIN
  157. SET @UserInfoDetail = 'Reason = You are not permitted to login at this time. Please, contact your administrator-:::-' + @UserInfoDetail
  158. EXEC proc_applicationLogs
  159. @flag = 'login',
  160. @logType = 'Login fails',
  161. @createdBy = @userName,
  162. @Reason = 'Not permitted to login at this time',
  163. @UserData = @UserData,
  164. @fieldValue = @UserInfoDetail,
  165. @agentId = @agentId,
  166. @IP = @ipAddress
  167. SET @errCode = '1'
  168. SET @autMsg = 'You are not permitted to login at this time1. Please, contact your administrator.'
  169. RETURN
  170. END
  171. IF EXISTS(SELECT TOP 1 'X' FROM userLockDetail WITH(NOLOCK)
  172. WHERE userId = @userId and GETDATE() BETWEEN startDate
  173. AND CONVERT(VARCHAR(20), endDate,101) + ' 23:59:59'
  174. AND ISNULL(isDeleted, 'N') = 'N')
  175. BEGIN
  176. SET @UserInfoDetail = 'Reason = You are not permitted to login at this time. Please, contact your administrator-:::-' + @UserInfoDetail
  177. EXEC proc_applicationLogs
  178. @flag = 'login',
  179. @logType = 'Login fails',
  180. @createdBy = @userName,
  181. @Reason = 'Not permitted to login for this date',
  182. @UserData = @UserData,
  183. @fieldValue = @UserInfoDetail,
  184. @agentId = @agentId,
  185. @IP = @ipAddress
  186. SET @errCode = '1'
  187. SET @autMsg = 'You are not permitted to login at this time. Please, contact your administrator.'
  188. RETURN
  189. END
  190. -- Last Login date check for Locking
  191. IF DATEDIFF(DAY, @lastLoginTs, GETDATE()) >= (SELECT TOP 1 ISNULL(lockUserDays, 30) FROM passwordFormat(NOLOCK) WHERE ISNULL(isActive, 'N') = 'Y')
  192. BEGIN
  193. UPDATE applicationUsers SET
  194. isLocked = 'Y'
  195. ,lastLoginTs = @GMTDate
  196. WHERE userId = @userId
  197. SET @UserInfoDetail = 'Reason = You are locked this time. Please, contact your administrator.-:::-' + @UserInfoDetail
  198. EXEC proc_applicationLogs
  199. @flag = 'login',
  200. @logType = 'Login fails',
  201. @createdBy = @userName,
  202. @Reason = 'Not Login for fix period, now user is locked',
  203. @UserData = @UserData,
  204. @fieldValue = @UserInfoDetail,
  205. @agentId = @agentId,
  206. @IP = @ipAddress
  207. INSERT INTO userLockHistory(username, lockReason, createdBy, createdDate)
  208. SELECT @userName, 'Your account has been locked due to not login for fix period', 'system', GETDATE()
  209. SET @errCode = '1'
  210. SET @autMsg = 'Your account has been locked due to not login for fix period.'
  211. RETURN;
  212. END
  213. -->> ON SUCCESS
  214. UPDATE applicationUsers SET
  215. lastLoginTs = @GMTDate
  216. WHERE [userName] = @userName
  217. --EXEC proc_applicationUsers @flag = 'userDetail', @userName = @userName
  218. --Audit data starts
  219. --EXEC proc_applicationLogs
  220. -- @flag='login',
  221. -- @logType='Login',
  222. -- @createdBy = @userName,
  223. -- @Reason='Agent Login',
  224. -- @UserData = @UserData,
  225. -- @fieldValue = @UserInfoDetail,
  226. -- @agentId=@agentId,
  227. -- @IP = @ipAddress
  228. --Audit data ends
  229. SET @errCode = '0'
  230. SET @autMsg = 'Success'
  231. RETURN
  232. END TRY
  233. BEGIN CATCH
  234. SET @errCode = '1'
  235. SET @autMsg = 'Authentication Fail,something went wrong !'
  236. END CATCH
  237. ---EXEC ws_proc_checkAuthntication 'admin','1swift+9','1001'
  238. GO