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.

274 lines
16 KiB

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