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.

344 lines
22 KiB

1 year ago
  1. USE [FastMoneyPro_Remit]
  2. GO
  3. /****** Object: StoredProcedure [dbo].[proc_fraudAnalysisLogin] 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].[proc_fraudAnalysisLogin](
  9. @flag VARCHAR(20)
  10. ,@fromDate VARCHAR(10) = NULL
  11. ,@toDate VARCHAR(10) = NULL
  12. ,@count INT = NULL
  13. ,@sCountry VARCHAR(30) = NULL
  14. ,@operator VARCHAR(30) = NULL
  15. ,@User VARCHAR(50)
  16. ,@agentId INT = NULL
  17. ,@userName VARCHAR(50) = NULL
  18. ,@agentConName VARCHAR(200) = null
  19. )AS
  20. SET NOCOUNT ON
  21. DECLARE @title VARCHAR(100)
  22. ------------------- START ######## SAME USER DIFF IP SUMMARY
  23. --select @fromDate = '2011-2-22', @toDate ='2014-2-22'
  24. DECLARE @SQL VARCHAR(MAX)
  25. SET @title = 'Fraud Login Analysis Report'
  26. -->>Same User Login by Multiple IP Address
  27. IF @flag = 'MIP'
  28. BEGIN
  29. SET @title = 'Fraud Login Analysis Report - SAME USER MULTIPLE IP'
  30. IF ISNULL(@count,0) = 0
  31. BEGIN
  32. SELECT 'Count parameter is missing' [Agent Name], NULL [Branch Name], NULL [Username], NULL [IP Count]
  33. END
  34. ELSE
  35. BEGIN
  36. SET @SQL = 'SELECT
  37. bm.agentId
  38. ,agentName = ISNULL(am.agentName, ''IME Head Office'')
  39. ,branchName = bm.agentName
  40. ,CreatedBy = l.CreatedBy
  41. ,IPCount = IP
  42. ,agentCountryId = am.agentCountryId
  43. FROM LoginLogs l WITH(NOLOCK)
  44. INNER JOIN agentMaster bm WITH(NOLOCK) ON l.agentId = bm.agentId
  45. LEFT JOIN agentMaster am WITH(NOLOCK) ON bm.parentId = am.agentId
  46. WHERE l.createdDate BETWEEN ''' + @fromDate + ''' AND ''' + @toDate + ' 23:59:59''
  47. AND (bm.agentRole = ''s'' OR bm.agentRole = ''b'' OR bm.agentRole IS NULL)
  48. GROUP BY bm.agentId, am.agentName, bm.AgentName, l.createdBy, l.IP, am.agentCountryId
  49. '
  50. IF ISNULL(@count,0) > 0
  51. BEGIN
  52. SET @SQL = '
  53. SELECT
  54. [Agent Name] = agentName
  55. ,[Branch Name] = branchName
  56. ,[Username] = CreatedBy
  57. ,[IP Count] = ''<a href="reports.aspx?reportName=10122200_login&reportBy=MIPDDN&sCountry='+ISNULL(@sCountry,'')+'&fromDate='+CONVERT(varchar,cast(@fromDate as date),101)+'&toDate='+CONVERT(varchar,cast(@toDate as date),101)+'&UserName=''+CreatedBy+''&agentId=''+CAST(AgentID AS VARCHAR)+''">''+CAST(count(IPCount) AS VARCHAR)+''</a>''
  58. FROM ( '
  59. + @SQL +
  60. ') x
  61. WHERE 1=1 '
  62. END
  63. IF @sCountry IS NOT NULL
  64. BEGIN
  65. SET @SQL = @SQL + ' AND agentCountryId = ''' + @sCountry + ''''
  66. END
  67. SET @SQL = @SQL + '
  68. GROUP BY agentName, branchName, createdBy, agentId
  69. HAVING COUNT(IPCount) ' + @operator + ' ' + CAST(@count AS VARCHAR) + '
  70. ORDER BY AgentName
  71. '
  72. --PRINT @SQL
  73. EXEC(@SQL)
  74. END
  75. END
  76. -->>Same User Login by Multiple IP Address DRILL DOWN
  77. ELSE IF @flag = 'MIPDDN'
  78. BEGIN
  79. --select top 100 * From LoginLogs where createdDate between '2014-2-24' and '2014-2-24'+' 23:59:59'
  80. SET @title = 'Fraud Login Analysis Report - DETAIL'
  81. SELECT DISTINCT
  82. [Agent Name] = agentName
  83. ,[Branch Name] = BranchName
  84. ,[Username] = CreatedBy
  85. ,[IP Address] = IP
  86. ,[DC Serial Number] = dcSerialNumber
  87. ,[DC User Name] = dcUserName
  88. ,[Login Date] = LoginDate
  89. ,[Log Type] = logType
  90. FROM (
  91. SELECT
  92. am.agentId
  93. ,agentName = 'IME HO'
  94. ,branchName = 'Head Office'
  95. ,l.CreatedBy
  96. ,IP
  97. ,dcSerialNumber
  98. ,dcUserName
  99. ,loginDate = l.createdDate
  100. ,logType
  101. FROM LoginLogs l WITH(NOLOCK)
  102. INNER JOIN agentMaster am WITH (NOLOCK) ON l.agentId = am.agentId
  103. Where l.createdDate BETWEEN @fromDate AND @toDate + ' 23:59:59'
  104. AND L.createdBy = @userName
  105. AND am.agentId = 1001
  106. UNION ALL
  107. SELECT
  108. am.agentId
  109. ,agentName = am.agentName
  110. ,branchName = bm.AgentName
  111. ,l.CreatedBy
  112. ,IP
  113. ,dcSerialNumber
  114. ,dcUserName
  115. ,loginDate = l.createdDate
  116. ,logType
  117. FROM LoginLogs l WITH(NOLOCK)
  118. INNER JOIN agentMaster bm WITH(NOLOCK) ON l.agentId = bm.agentId
  119. INNER JOIN agentMaster am WITH(NOLOCK) ON bm.parentId = am.agentId
  120. WHERE l.createdDate BETWEEN @fromDate AND @toDate+' 23:59:59'
  121. AND L.createdBy = @userName
  122. AND bm.agentId = @agentId
  123. ) x
  124. ORDER BY LoginDate
  125. END
  126. -->>Same User Login by Multiple Certificate
  127. ELSE IF @flag = 'MCert'
  128. BEGIN
  129. SET @title = 'Fraud Login Analysis Report - SAME USER MULTIPLE CERTIFICATE'
  130. -- ############## SAME USER DIFF Certificate
  131. IF ISNULL(@count,0) = 0
  132. BEGIN
  133. SELECT 'Count parameter is missing' [Agent Name], NULL [Branch Name], NULL [Username], NULL [IP Count]
  134. END
  135. ELSE
  136. BEGIN
  137. SET @SQL = '
  138. SELECT
  139. bm.agentId
  140. ,agentName = ISNULL(am.agentName, ''IME Head Office'')
  141. ,branchName = bm.agentName
  142. ,createdBy = l.CreatedBy
  143. ,IPCount = dcSerialNumber
  144. ,agentCountryId = am.agentCountryId
  145. FROM LoginLogs l WITH(NOLOCK)
  146. INNER JOIN agentMaster bm WITH(NOLOCK) ON l.agentId = bm.agentId
  147. LEFT JOIN agentMaster am WITH(NOLOCK) ON bm.parentId = am.agentId
  148. WHERE l.createdDate BETWEEN ''' + @fromDate + ''' AND ''' + @toDate + ' 23:59:59''
  149. AND (bm.agentRole = ''s'' OR bm.agentRole = ''b'' OR bm.agentRole IS NULL)
  150. GROUP BY bm.agentId, am.agentName, bm.AgentName, l.createdBy, l.dcSerialNumber, am.agentCountryId
  151. '
  152. IF ISNULL(@count,0) > 0
  153. BEGIN
  154. SET @SQL = '
  155. SELECT
  156. [Agent Name] = agentName
  157. ,[Branch Name] = branchName
  158. ,[Username] = CreatedBy
  159. ,[Cert. Count] = ''<a href="reports.aspx?reportName=10122200_login&reportBy=MIPDDN&sCountry='+ISNULL(@sCountry,'')+'&fromDate='+CONVERT(varchar,cast(@fromDate as date),101)+'&toDate='+CONVERT(varchar,cast(@toDate as date),101)+'&UserName=''+CreatedBy+''&agentId=''+CAST(AgentID AS VARCHAR)+''">''+CAST(count(IPCount) AS VARCHAR)+''</a>''
  160. FROM ( '
  161. + @SQL +
  162. ') x
  163. WHERE 1=1 '
  164. END
  165. IF @sCountry IS NOT NULL
  166. BEGIN
  167. SET @SQL = @SQL + ' AND agentCountryId = ''' + @sCountry + ''''
  168. END
  169. SET @SQL = @SQL + '
  170. GROUP BY agentName, branchName, createdBy, agentId
  171. HAVING COUNT(IPCount) ' + @operator + ' ' + CAST(@count AS VARCHAR) + '
  172. ORDER BY AgentName
  173. '
  174. --PRINT @SQL
  175. EXEC(@SQL)
  176. END
  177. END
  178. ELSE IF @flag = 'MCertDdl'
  179. BEGIN
  180. SET @title = 'Fraud Login Analysis Report - SAME USER MULTIPLE CERTIFICATE DETAIL'
  181. -- ############## Multiple Login Attempts
  182. select AgentName ,BranchName,CreatedBy UserName, IPCount attemptCount from (
  183. SELECT distinct a.AgentID,b.AgentName,a.agentName BranchName,
  184. l.CreatedBy,dcSerialNumber as IPCount
  185. from LoginLogs l with (nolock)
  186. inner join agentMaster a with (nolock) on l.agentId=a.agentId
  187. inner join agentMaster b with (nolock) on a.ParentId=b.agentId
  188. Where l.createdDate between @fromDate and @toDate+' 23:59:59'
  189. AND L.createdBy = @userName
  190. AND A.agentId = @agentId
  191. UNION ALL
  192. SELECT distinct a.AgentID,b.AgentName,a.AgentName,l.CreatedBy,dcSerialNumber as IPCount
  193. from LoginLogs l with (nolock)
  194. inner join agentMaster a with (nolock) on l.agentId=a.agentId
  195. inner join agentMaster b with (nolock) on a.agentId = b.parentId
  196. Where l.createdDate between @fromDate and @toDate+' 23:59:59'
  197. AND L.createdBy = @userName
  198. AND A.agentId = @agentId
  199. ) x
  200. group by AgentName,CreatedBy, IPCount,BranchName
  201. order by IPCount
  202. End
  203. -->>Failed login Attempt
  204. ELSE IF @flag = 'FLogin'
  205. BEGIN
  206. SET @title = 'Fraud Login Analysis Report - FAIL LOGIN ATTEMPT'
  207. IF ISNULL(@count,0) = 0
  208. BEGIN
  209. select 'Count parameter is missing' AgentName,NULL CreatedBy,NULL IPCount
  210. END
  211. ELSE
  212. BEGIN
  213. SET @SQL = '
  214. SELECT
  215. bm.agentId
  216. ,agentName = ISNULL(am.agentName, ''IME Head Office'')
  217. ,branchName = bm.AgentName
  218. ,l.CreatedBy
  219. ,am.agentCountryId
  220. ,CNT = COUNT(l.CreatedBy)
  221. FROM LoginLogs l WITH(NOLOCK)
  222. INNER JOIN agentMaster bm WITH(NOLOCK) ON l.agentId = bm.agentId
  223. LEFT JOIN agentMaster am WITH(NOLOCK) ON bm.parentId = am.agentId
  224. WHERE l.createdDate between ''' + @fromDate + ''' AND ''' + @toDate + ' 23:59:59''
  225. AND logType = ''Login fails''
  226. AND (bm.agentRole = ''s'' OR bm.agentRole = ''b'' OR bm.agentRole IS NULL)
  227. GROUP BY bm.agentId, am.AgentName, bm.agentName, l.CreatedBy, am.agentCountryId
  228. '
  229. IF ISNULL(@count,0) > 0
  230. BEGIN
  231. SET @SQL = '
  232. SELECT
  233. [Agent Name] = agentName
  234. ,[Branch Name] = branchName
  235. ,[Username] = CreatedBy
  236. ,[Fail Count] = ''<a href="reports.aspx?reportName=10122200_login&reportBy=MIPDDN&sCountry='+ISNULL(@sCountry,'')+'&fromDate='+CONVERT(varchar,cast(@fromDate as date),101)+'&toDate='+CONVERT(varchar,cast(@toDate as date),101)+'&UserName=''+CreatedBy+''&agentId=''+CAST(AgentID AS VARCHAR)+''">''+CAST(SUM(CNT) AS VARCHAR)+''</a>''
  237. FROM ( '
  238. + @SQL +
  239. ') x
  240. WHERE 1=1 '
  241. END
  242. IF @sCountry IS NOT NULL
  243. BEGIN
  244. SET @SQL = @SQL + ' AND agentCountryId = ''' + @sCountry + ''''
  245. END
  246. SET @SQL = @SQL + '
  247. GROUP BY agentName, branchName, createdBy, agentId
  248. HAVING SUM(CNT) ' + @operator + ' ' + CAST(@count AS VARCHAR) + '
  249. ORDER BY agentName
  250. '
  251. --PRINT @SQL
  252. EXEC(@SQL)
  253. END
  254. END
  255. -->>Login Frequency
  256. ELSE IF @flag = 'LoginFreq'
  257. BEGIN
  258. SET @title = 'Fraud Login Analysis Report - LOGIN FREQUENCY'
  259. IF ISNULL(@count,0) = 0
  260. BEGIN
  261. select 'Count parameter is missing' AgentName,NULL CreatedBy,NULL IPCount
  262. END
  263. ELSE
  264. BEGIN
  265. SET @SQL = 'SELECT
  266. a.AgentID,ISNULL(b.AgentName, '' HO User'') AgentName,l.CreatedBy,B.agentCountryId
  267. ,SCNT = CASE WHEN logType =''Login'' THEN COUNT(l.CreatedBy) ELSE 0 END
  268. ,FCNT = CASE WHEN logType =''Login fails'' THEN COUNT(l.CreatedBy) ELSE 0 END
  269. from LoginLogs l with (nolock)
  270. inner join agentMaster a with (nolock) on l.agentId = a.agentId
  271. inner join agentMaster b with (nolock) on a.ParentId = b.agentId
  272. Where l.createdDate between '''+@fromDate+''' and '''+@toDate+' 23:59:59''
  273. and (a.agentRole =''s'' OR a.agentRole =''b'' OR a.agentRole is null)
  274. group by a.AgentID,b.AgentName,l.CreatedBy,B.agentCountryId,logType
  275. '
  276. IF ISNULL(@count,0) > 0
  277. BEGIN
  278. SET @SQL = ' select AgentName,CreatedBy UserName
  279. ,[Success Count] = ''<a href="reports.aspx?reportName=10122200_login&reportBy=MIPDDN&sCountry='+ISNULL(@sCountry,'')+'&fromDate='+@fromDate+'&toDate='+@toDate+'&UserName=''+CreatedBy+''&agentId=''+CAST(AgentID AS VARCHAR)+''">''+CAST(SUM(SCNT) AS VARCHAR)+''</a>''
  280. ,[Fail Count] = ''<a href="reports.aspx?reportName=10122200_login&reportBy=MIPDDN&sCountry='+ISNULL(@sCountry,'')+'&fromDate='+@fromDate+'&toDate='+@toDate+'&UserName=''+CreatedBy+''&agentId=''+CAST(AgentID AS VARCHAR)+''">''+CAST(SUM(FCNT) AS VARCHAR)+''</a>''
  281. from ( '
  282. + @SQL
  283. +') x
  284. WHERE 1=1 '
  285. END
  286. IF @sCountry IS NOT NULL
  287. BEGIN
  288. SET @SQL = @SQL + ' AND agentCountryId='''+@sCountry+''''
  289. END
  290. SET @SQL = @SQL + ' group by AgentName,CreatedBy,AgentID
  291. HAVING SUM(SCNT) '+@operator+' '+CAST( @count AS VARCHAR)+'
  292. AND SUM(FCNT) '+@operator+' '+CAST( @count AS VARCHAR)+'
  293. order by AgentName '
  294. --PRINT @SQL
  295. EXEC(@SQL)
  296. END
  297. end
  298. EXEC proc_errorHandler '0', 'Report has been prepared successfully.', NULL
  299. SELECT 'From Date' head, @fromDate value
  300. UNION ALL
  301. SELECT 'To Date' head, @toDate value
  302. SELECT @title title
  303. GO