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.

2265 lines
76 KiB

1 year ago
  1. USE [FastMoneyPro_Remit]
  2. GO
  3. /****** Object: StoredProcedure [dbo].[proc_applicationUsers] Script Date: 7/4/2019 11:35:48 AM ******/
  4. DROP PROCEDURE [dbo].[proc_applicationUsers]
  5. GO
  6. /****** Object: StoredProcedure [dbo].[proc_applicationUsers] Script Date: 7/4/2019 11:35:48 AM ******/
  7. SET ANSI_NULLS ON
  8. GO
  9. SET QUOTED_IDENTIFIER ON
  10. GO
  11. CREATE PROC [dbo].[proc_applicationUsers]
  12. @flag VARCHAR(50) = NULL
  13. ,@userId INT = NULL
  14. ,@user VARCHAR(30) = NULL
  15. ,@userName VARCHAR(30) = NULL
  16. ,@agentName VARCHAR(100) = NULL
  17. ,@agentCode VARCHAR(20) = NULL
  18. ,@firstName VARCHAR(30) = NULL
  19. ,@middleName VARCHAR(30) = NULL
  20. ,@lastName VARCHAR(30) = NULL
  21. ,@salutation VARCHAR(10) = NULL
  22. ,@gender VARCHAR(10) = NULL
  23. ,@telephoneNo VARCHAR(15) = NULL
  24. ,@address VARCHAR(50) = NULL
  25. ,@city VARCHAR(30) = NULL
  26. ,@countryId INT = NULL
  27. ,@countryName VARCHAR(100) = NULL
  28. ,@state INT = NULL
  29. ,@district INT = NULL
  30. ,@zip VARCHAR(10) = NULL
  31. ,@mobileNo VARCHAR(15) = NULL
  32. ,@email VARCHAR(255) = NULL
  33. ,@pwd VARCHAR(255) = NULL
  34. ,@isActive CHAR(1) = NULL
  35. ,@isLocked CHAR(1) = NULL
  36. ,@agentId INT = NULL
  37. ,@sessionTimeOutPeriod INT = NULL
  38. ,@tranApproveLimit MONEY = NULL
  39. ,@agentCrLimitAmt MONEY = NULL
  40. ,@loginTime VARCHAR(10) = NULL
  41. ,@logoutTime VARCHAR(10) = NULL
  42. ,@userAccessLevel CHAR(1) = NULL
  43. ,@perDayTranLimit INT = NULL
  44. ,@fromSendTrnTime TIME = NULL
  45. ,@toSendTrnTime TIME = NULL
  46. ,@fromPayTrnTime TIME = NULL
  47. ,@toPayTrnTime TIME = NULL
  48. ,@fromRptViewTime TIME = NULL
  49. ,@toRptViewTime TIME = NULL
  50. ,@isDeleted CHAR(1) = NULL
  51. ,@approvedDate DATETIME = NULL
  52. ,@lastLoginTs DATETIME = NULL
  53. ,@pwdChangeDays INT = NULL
  54. ,@pwdChangeWarningDays INT = NULL
  55. ,@lastPwdChangedOn DATETIME = NULL
  56. ,@forceChangePwd CHAR(1) = NULL
  57. ,@oldPwd VARCHAR(255) = NULL
  58. ,@name VARCHAR(50) = NULL
  59. ,@file VARCHAR(500) = NULL
  60. ,@changesApprovalQueueRowId BIGINT = NULL
  61. ,@haschanged CHAR(1) = NULL
  62. ,@sortBy VARCHAR(50) = NULL
  63. ,@sortOrder VARCHAR(5) = NULL
  64. ,@pageSize INT = NULL
  65. ,@pageNumber INT = NULL
  66. ,@UserInfoDetail VARCHAR(MAX) = NULL
  67. ,@maxReportViewDays INT = NULL
  68. ,@lockReason VARCHAR(500) = NULL
  69. ,@employeeId VARCHAR(10) = NULL
  70. ,@userType VARCHAR(2) = NULL
  71. ,@txnPwd VARCHAR(255) = NULL
  72. AS
  73. /*
  74. @flag
  75. s = select all (with dynamic filters)
  76. i = insert
  77. u = update
  78. a = select by role id
  79. d = delete by role id
  80. l = login
  81. r = reset password --@custodian_id, @user, @pwd
  82. cp = change password --@user, @pwd, @oldPwd
  83. loc = Lock
  84. cu = check user
  85. lo = Log Out
  86. [custodian]
  87. */
  88. SET NOCOUNT ON;
  89. SET XACT_ABORT ON;
  90. BEGIN TRY
  91. CREATE TABLE #msg(errorCode INT, msg VARCHAR(100), id INT)
  92. DECLARE
  93. @sql VARCHAR(MAX)
  94. ,@oldValue VARCHAR(MAX)
  95. ,@newValue VARCHAR(MAX)
  96. ,@tableName VARCHAR(50)
  97. ,@logIdentifier VARCHAR(100)
  98. ,@logParamMain VARCHAR(100)
  99. ,@tableAlias VARCHAR(100)
  100. ,@modType VARCHAR(6)
  101. ,@module INT
  102. ,@select_field_list VARCHAR(MAX)
  103. ,@extra_field_list VARCHAR(MAX)
  104. ,@table VARCHAR(MAX)
  105. ,@sql_filter VARCHAR(MAX)
  106. ,@ApprovedFunctionId INT
  107. ,@msg VARCHAR(200)
  108. ,@parentAgentId int
  109. SELECT
  110. @logIdentifier = 'userId'
  111. ,@logParamMain = 'applicationUsers'
  112. ,@tableAlias = 'User Setup'
  113. ,@module = 10
  114. ,@ApprovedFunctionId = 10101130
  115. IF @userId is null
  116. SELECT @userId = userId FROM applicationUsers WITH(NOLOCK)
  117. WHERE userName = @user
  118. if @parentAgentId is null
  119. select @parentAgentId=parentId from agentMaster WITH(NOLOCK)
  120. where agentId=@agentId
  121. IF @flag = 'an'
  122. BEGIN
  123. SELECT agentName FROM agentMaster WHERE
  124. agentId = (SELECT agentId FROM applicationUsers WITH(NOLOCK) WHERE userName = @user)
  125. END
  126. IF @flag = 'HO' ---## POPULATE HO USER
  127. BEGIN
  128. select userName from applicationUsers a with(nolock) inner join agentMaster b with(nolock)
  129. on a.agentId=b.agentId where b.agentType=2901 and
  130. a.isActive='Y' and ISNULL(a.isDeleted,'N')<>'Y' order by userName
  131. END
  132. IF @flag = 'agent' ---## POPULATE AGENT USER
  133. BEGIN
  134. select userName,B.agentType from applicationUsers a with(nolock) inner join agentMaster b with(nolock)
  135. on a.agentId=b.agentId where b.agentType<>2901 and
  136. a.isActive='Y' and ISNULL(a.isDeleted,'N')<>'Y' order by userName
  137. END
  138. ELSE IF @flag = 'lu' --lu - Lock/Unlock
  139. BEGIN
  140. SELECT @userName = userName, @isLocked = isLocked FROM applicationUsers WITH(NOLOCK) WHERE userId = @userId
  141. UPDATE applicationUsers SET
  142. isLocked = CASE WHEN @isLocked = 'N' THEN 'Y' ELSE 'N' END
  143. WHERE userId = @userId
  144. IF @isLocked = 'Y'
  145. BEGIN
  146. UPDATE applicationUsers SET invalidAttemptCount=0 WHERE userId=@userId
  147. SET @msg = 'User with Username ' + @userName + ' unlocked successfully'
  148. EXEC proc_errorHandler 0, @msg, @userId
  149. INSERT INTO userLockHistory(username, lockReason, createdBy, createdDate)
  150. SELECT @userName, 'User account unlocked successfully', @user, GETDATE()
  151. END
  152. ELSE
  153. BEGIN
  154. SET @msg = 'User with Username ' + @userName + ' locked successfully'
  155. EXEC proc_errorHandler 0, @msg, @userId
  156. INSERT INTO userLockHistory(username, lockReason, createdBy, createdDate)
  157. SELECT @userName, 'Account locked by administrator', @user, GETDATE()
  158. END
  159. END
  160. ELSE IF @flag = 'lr'
  161. BEGIN
  162. SELECT TOP 1
  163. createdBy
  164. ,createdDate
  165. ,lockReason
  166. FROM userLockHistory WITH(NOLOCK)
  167. WHERE username = @userName
  168. ORDER BY ulhId DESC
  169. END
  170. ELSE IF @flag = 'userDetail'
  171. BEGIN
  172. DECLARE
  173. @branch INT, @branchName VARCHAR(100), @agent INT, @superAgent INT, @superAgentName VARCHAR(100),
  174. @mapCodeInt VARCHAR(8), @parentMapCodeInt VARCHAR(8), @agentType INT, @settlingAgent INT, @parentId INT, @actAsBranch CHAR(1),
  175. @mapCodeDom VARCHAR(8)
  176. SELECT @branch = agentId FROM applicationUsers WITH(NOLOCK) WHERE userName = @userName
  177. SELECT
  178. @branchName = agentName, @agentName = agentName,
  179. @mapCodeInt = mapCodeInt, @parentmapCodeInt = mapCodeInt, @mapCodeDom = mapCodeDom,
  180. @agentType = agentType, @actAsBranch = actAsBranch, @agent = parentId, @superAgent = parentId, @parentId = parentId
  181. FROM agentMaster WITH(NOLOCK) WHERE agentId = @branch
  182. IF @branch <> dbo.FNAGetHOAgentId()
  183. BEGIN
  184. IF(@agentType = 2903)
  185. BEGIN
  186. SET @agent = @branch
  187. END
  188. ELSE
  189. BEGIN
  190. SELECT @agentName = agentName, @parentMapCodeInt = mapCodeInt, @superAgent = parentId FROM agentMaster WITH(NOLOCK) WHERE agentId = @agent
  191. END
  192. SELECT @superAgentName = agentName FROM agentMaster WITH(NOLOCK) WHERE agentId = @superAgent
  193. SELECT @settlingAgent = agentId FROM agentMaster WITH(NOLOCK) WHERE agentId = @branch AND isSettlingAgent = 'Y'
  194. IF @settlingAgent IS NULL
  195. SELECT @settlingAgent = agentId FROM agentMaster WITH(NOLOCK) WHERE agentId = @agent AND isSettlingAgent = 'Y'
  196. IF @settlingAgent IS NULL
  197. SELECT @settlingAgent = agentId FROM agentMaster WITH(NOLOCK) WHERE agentId = @superAgent AND isSettlingAgent = 'Y'
  198. END
  199. SELECT
  200. au.*
  201. ,fullName = au.firstName + ISNULL(' ' + au.middleName, '') + ISNULL(' ' + au.lastName, '')
  202. ,parentId = ISNULL(@parentId, 0)
  203. ,agentType = ISNULL(@agentType, 2901)
  204. ,settlingAgent = ISNULL(@settlingAgent, 0)
  205. ,actAsBranch = ISNULL(@actAsBranch, 'N')
  206. ,mapCodeInt = ISNULL(@mapCodeInt, '0000')
  207. ,parentMapCodeInt = ISNULL(@parentMapCodeInt, '0000')
  208. ,mapCodeDom = ISNULL(@mapCodeDom, '0000')
  209. ,branch = @branch
  210. ,branchName = @branchName
  211. ,agent = @agent
  212. ,agentName = @agentName
  213. ,superAgent = ISNULL(@superAgent, 0)
  214. ,superAgentName = ISNULL(@superAgentName, 0)
  215. FROM applicationUsers au WITH(NOLOCK)
  216. WHERE au.userName = @userName
  217. END
  218. IF @flag IN ('hs')
  219. BEGIN
  220. DECLARE @hasRight CHAR(1)
  221. SET @hasRight = dbo.FNAHasRight(@user, CAST(@ApprovedFunctionId AS VARCHAR))
  222. SET @table = '(
  223. SELECT
  224. userId = ISNULL(aum.userId, au.userId)
  225. ,[userName] = ISNULL(aum.userName, au.userName)
  226. ,agentCode = ISNULL(aum.agentCode, au.agentCode)
  227. ,firstName = ISNULL(aum.firstName, au.firstName)
  228. ,middleName = ISNULL(aum.middleName, au.middleName)
  229. ,lastName = ISNULL(aum.lastName, au.lastName)
  230. ,salutation = ISNULL(aum.salutation, au.salutation)
  231. ,gender = ISNULL(aum.gender, au.gender)
  232. ,[address] = ISNULL(aum.address, au.address)
  233. ,city = ISNULL(aum.city, au.city)
  234. ,countryId = ISNULL(aum.countryId, au.countryId)
  235. ,state = ISNULL(aum.state, au.state)
  236. ,district = ISNULL(aum.district, au.district)
  237. ,zip = ISNULL(aum.zip, au.zip)
  238. ,telephoneNo = ISNULL(aum.telephoneNo, au.telephoneNo)
  239. ,mobileNo = ISNULL(aum.mobileNo, au.mobileNo)
  240. ,email = ISNULL(aum.email, au.email)
  241. ,pwd = ISNULL(aum.pwd, au.pwd)
  242. ,isActive = ISNULL(aum.isActive, au.isActive)
  243. ,isLocked = au.isLocked
  244. ,agentId = ISNULL(aum.agentId, au.agentId)
  245. ,sessionTimeOutPeriod = ISNULL(aum.sessionTimeOutPeriod, au.sessionTimeOutPeriod)
  246. ,loginTime = ISNULL(aum.loginTime, au.loginTime)
  247. ,logoutTime = ISNULL(aum.logoutTime, au.logoutTime)
  248. ,userAccessLevel = ISNULL(aum.userAccessLevel, au.userAccessLevel)
  249. ,lastLoginTs = ISNULL(aum.lastLoginTs, au.lastLoginTs)
  250. ,pwdChangeDays = ISNULL(aum.pwdChangeDays, au.pwdChangeDays)
  251. ,pwdChangeWarningDays = ISNULL(aum.pwdChangeWarningDays, au.pwdChangeWarningDays)
  252. ,lastPwdChangedOn = ISNULL(aum.lastPwdChangedOn, au.lastPwdChangedOn)
  253. ,forceChangePwd = ISNULL(aum.forceChangePwd, au.forceChangePwd)
  254. ,maxReportViewDays = ISNULL(aum.maxReportViewDays, au.maxReportViewDays)
  255. ,au.createdBy
  256. ,au.createdDate
  257. ,modifiedDate = CASE WHEN au.approvedBy IS NULL THEN au.createdDate ELSE aum.createdDate END
  258. ,modifiedBy = CASE WHEN au.approvedBy IS NULL THEN au.createdBy ELSE aum.createdBy END
  259. ,hasChanged = CASE WHEN (au.approvedBy IS NULL) OR
  260. (aum.userId IS NOT NULL) OR
  261. (x.userId IS NOT NULL) OR
  262. (y.userId IS NOT NULL)
  263. THEN ''Y'' ELSE ''N'' END
  264. FROM
  265. applicationUsers au with(nolock)
  266. LEFT JOIN (
  267. SELECT
  268. userId
  269. ,createdBy = MAX(createdBy)
  270. ,createdDate = MAX(createdDate)
  271. FROM applicationUserFunctionsMod aufm WITH(NOLOCK)
  272. GROUP BY userId
  273. ) x ON au.userId = x.userId
  274. LEFT JOIN (
  275. SELECT
  276. userId
  277. ,createdBy = MAX(createdBy)
  278. ,createdDate = MAX(createdDate)
  279. FROM applicationUserRolesMod aurm WITH(NOLOCK)
  280. GROUP BY userId
  281. ) y ON au.userId = y.userId
  282. LEFT JOIN applicationUsersMod aum ON au.userId = aum.userId
  283. AND (
  284. aum.createdBy = ''' + @user + '''
  285. OR ''Y'' = ''' + @hasRight + '''
  286. )
  287. WHERE ISNULL(au.isDeleted, ''N'') <> ''Y''
  288. AND (
  289. au.approvedBy IS NOT NULL
  290. OR au.createdBy = ''' + @user + '''
  291. OR ''Y'' = ''' + @hasRight + '''
  292. )
  293. ) '
  294. END
  295. ELSE IF @flag IN ('s','t')
  296. BEGIN
  297. SET @hasRight = dbo.FNAHasRight(@user, CAST(@ApprovedFunctionId AS VARCHAR))
  298. IF (@user IN ('admin', 'admin1'))
  299. BEGIN
  300. SET @table = '(
  301. SELECT
  302. userId = ISNULL(aum.userId, au.userId)
  303. ,[userName] = ISNULL(aum.userName, au.userName)
  304. ,agentCode = ISNULL(aum.agentCode, au.agentCode)
  305. ,userType = ISNULL(aum.userType, au.userType)
  306. ,firstName = ISNULL(aum.firstName, au.firstName)
  307. ,middleName = ISNULL(aum.middleName, au.middleName)
  308. ,lastName = ISNULL(aum.lastName, au.lastName)
  309. ,salutation = ISNULL(aum.salutation, au.salutation)
  310. ,gender = ISNULL(aum.gender, au.gender)
  311. ,[address] = ISNULL(aum.address, au.address)
  312. ,city = ISNULL(aum.city, au.city)
  313. ,countryId = ISNULL(aum.countryId, au.countryId)
  314. ,state = ISNULL(aum.state, au.state)
  315. ,district = ISNULL(aum.district, au.district)
  316. ,zip = ISNULL(aum.zip, au.zip)
  317. ,telephoneNo = ISNULL(aum.telephoneNo, au.telephoneNo)
  318. ,mobileNo = ISNULL(aum.mobileNo, au.mobileNo)
  319. ,email = ISNULL(aum.email, au.email)
  320. ,pwd = ISNULL(aum.pwd, au.pwd)
  321. ,isActive = ISNULL(aum.isActive, au.isActive)
  322. ,isLocked = au.isLocked
  323. ,agentId = ISNULL(aum.agentId, au.agentId)
  324. ,sessionTimeOutPeriod = ISNULL(aum.sessionTimeOutPeriod, au.sessionTimeOutPeriod)
  325. ,loginTime = ISNULL(aum.loginTime, au.loginTime)
  326. ,logoutTime = ISNULL(aum.logoutTime, au.logoutTime)
  327. ,userAccessLevel = ISNULL(aum.userAccessLevel, au.userAccessLevel)
  328. ,lastLoginTs = ISNULL(aum.lastLoginTs, au.lastLoginTs)
  329. ,pwdChangeDays = ISNULL(aum.pwdChangeDays, au.pwdChangeDays)
  330. ,pwdChangeWarningDays = ISNULL(aum.pwdChangeWarningDays, au.pwdChangeWarningDays)
  331. ,lastPwdChangedOn = ISNULL(aum.lastPwdChangedOn, au.lastPwdChangedOn)
  332. ,forceChangePwd = ISNULL(aum.forceChangePwd, au.forceChangePwd)
  333. ,maxReportViewDays = ISNULL(aum.maxReportViewDays, au.maxReportViewDays)
  334. ,employeeId = au.employeeId
  335. ,au.createdBy
  336. ,au.createdDate
  337. ,modifiedDate = CASE WHEN au.approvedBy IS NULL THEN au.createdDate ELSE aum.createdDate END
  338. ,modifiedBy = CASE WHEN au.approvedBy IS NULL THEN au.createdBy ELSE aum.createdBy END
  339. ,hasChanged = CASE WHEN (au.approvedBy IS NULL) OR
  340. (aum.userId IS NOT NULL) OR
  341. (x.userId IS NOT NULL) OR
  342. (y.userId IS NOT NULL)
  343. THEN ''Y'' ELSE ''N'' END
  344. FROM
  345. applicationUsers au with(nolock)
  346. LEFT JOIN (
  347. SELECT
  348. userId
  349. ,createdBy = MAX(createdBy)
  350. ,createdDate = MAX(createdDate)
  351. FROM applicationUserFunctionsMod aufm WITH(NOLOCK)
  352. GROUP BY userId
  353. ) x ON au.userId = x.userId
  354. LEFT JOIN (
  355. SELECT
  356. userId
  357. ,createdBy = MAX(createdBy)
  358. ,createdDate = MAX(createdDate)
  359. FROM applicationUserRolesMod aurm WITH(NOLOCK)
  360. GROUP BY userId
  361. ) y ON au.userId = y.userId
  362. LEFT JOIN applicationUsersMod aum ON au.userId = aum.userId
  363. AND (
  364. aum.createdBy = ''' + @user + '''
  365. OR ''Y'' = ''' + @hasRight + '''
  366. )
  367. WHERE ISNULL(au.isDeleted, ''N'') <> ''Y''
  368. AND (
  369. au.approvedBy IS NOT NULL
  370. OR au.createdBy = ''' + @user + '''
  371. OR ''Y'' = ''' + @hasRight + '''
  372. )
  373. ) '
  374. END
  375. ELSE
  376. BEGIN
  377. IF OBJECT_ID('tempdb..#userId') IS NOT NULL
  378. DROP TABLE #userId
  379. CREATE TABLE #userId(userId INT)
  380. INSERT INTO #userId
  381. SELECT userId FROM applicationUsers WITH(NOLOCK) WHERE ISNULL(isDeleted, 'N') = 'N'
  382. DELETE FROM #userId
  383. FROM #userId ui
  384. INNER JOIN
  385. userGroupMapping ugm ON ui.userId = ugm.userId
  386. WHERE ugm.groupCat = '6900' AND ISNULL(ugm.isDeleted, 'N') = 'N'
  387. INSERT INTO #userId
  388. SELECT DISTINCT userId FROM applicationUsers au WITH(NOLOCK)
  389. INNER JOIN(
  390. SELECT DISTINCT agm.agentId FROM agentGroupMaping agm
  391. WHERE agm.groupDetail IN (SELECT groupDetail FROM userGroupMapping WHERE userName = @user AND ISNULL(isDeleted, 'N') = 'N')
  392. AND ISNULL(agm.isDeleted, 'N') = 'N'
  393. )x ON au.agentId = x.agentId
  394. SET @table = '(
  395. SELECT
  396. userId = ISNULL(aum.userId, au.userId)
  397. ,[userName] = ISNULL(aum.userName, au.userName)
  398. ,agentCode = ISNULL(aum.agentCode, au.agentCode)
  399. ,userType = ISNULL(aum.userType, au.userType)
  400. ,firstName = ISNULL(aum.firstName, au.firstName)
  401. ,middleName = ISNULL(aum.middleName, au.middleName)
  402. ,lastName = ISNULL(aum.lastName, au.lastName)
  403. ,salutation = ISNULL(aum.salutation, au.salutation)
  404. ,gender = ISNULL(aum.gender, au.gender)
  405. ,[address] = ISNULL(aum.address, au.address)
  406. ,city = ISNULL(aum.city, au.city)
  407. ,countryId = ISNULL(aum.countryId, au.countryId)
  408. ,state = ISNULL(aum.state, au.state)
  409. ,district = ISNULL(aum.district, au.district)
  410. ,zip = ISNULL(aum.zip, au.zip)
  411. ,telephoneNo = ISNULL(aum.telephoneNo, au.telephoneNo)
  412. ,mobileNo = ISNULL(aum.mobileNo, au.mobileNo)
  413. ,email = ISNULL(aum.email, au.email)
  414. ,pwd = ISNULL(aum.pwd, au.pwd)
  415. ,isActive = ISNULL(aum.isActive, au.isActive)
  416. ,isLocked = au.isLocked
  417. ,agentId = ISNULL(aum.agentId, au.agentId)
  418. ,sessionTimeOutPeriod = ISNULL(aum.sessionTimeOutPeriod, au.sessionTimeOutPeriod)
  419. ,loginTime = ISNULL(aum.loginTime, au.loginTime)
  420. ,logoutTime = ISNULL(aum.logoutTime, au.logoutTime)
  421. ,userAccessLevel = ISNULL(aum.userAccessLevel, au.userAccessLevel)
  422. ,lastLoginTs = ISNULL(aum.lastLoginTs, au.lastLoginTs)
  423. ,pwdChangeDays = ISNULL(aum.pwdChangeDays, au.pwdChangeDays)
  424. ,pwdChangeWarningDays = ISNULL(aum.pwdChangeWarningDays, au.pwdChangeWarningDays)
  425. ,lastPwdChangedOn = ISNULL(aum.lastPwdChangedOn, au.lastPwdChangedOn)
  426. ,forceChangePwd = ISNULL(aum.forceChangePwd, au.forceChangePwd)
  427. ,maxReportViewDays = ISNULL(aum.maxReportViewDays, au.maxReportViewDays)
  428. ,au.employeeId
  429. ,au.createdBy
  430. ,au.createdDate
  431. ,modifiedDate = CASE WHEN au.approvedBy IS NULL THEN au.createdDate ELSE aum.createdDate END
  432. ,modifiedBy = CASE WHEN au.approvedBy IS NULL THEN au.createdBy ELSE aum.createdBy END
  433. ,hasChanged = CASE WHEN (au.approvedBy IS NULL) OR
  434. (aum.userId IS NOT NULL) OR
  435. (x.userId IS NOT NULL) OR
  436. (y.userId IS NOT NULL)
  437. THEN ''Y'' ELSE ''N'' END
  438. FROM applicationUsers au WITH(NOLOCK)
  439. INNER JOIN
  440. (
  441. --SELECT * FROM userGroupMapping
  442. --SELECT aum.* FROM userGroupMapping ugm
  443. --inner join agentGroupMaping agm on ugm.groupDetail=agm.groupDetail
  444. --inner join applicationUsers aum on aum.userId = ugm.userId
  445. --where a.userName= ''' + @user + '''
  446. SELECT DISTINCT userId FROM #userId
  447. ) u ON au.userId = u.userId
  448. LEFT JOIN (
  449. SELECT
  450. userId
  451. ,createdBy = MAX(createdBy)
  452. ,createdDate = MAX(createdDate)
  453. FROM applicationUserFunctionsMod aufm WITH(NOLOCK)
  454. GROUP BY userId
  455. ) x ON au.userId = x.userId
  456. LEFT JOIN (
  457. SELECT
  458. userId
  459. ,createdBy = MAX(createdBy)
  460. ,createdDate = MAX(createdDate)
  461. FROM applicationUserRolesMod aurm WITH(NOLOCK)
  462. GROUP BY userId
  463. ) y ON au.userId = y.userId
  464. LEFT JOIN applicationUsersMod aum ON au.userId = aum.userId
  465. AND (
  466. aum.createdBy = ''' + @user + '''
  467. OR ''Y'' = ''' + @hasRight + '''
  468. )
  469. WHERE ISNULL(au.isDeleted, ''N'') <> ''Y''
  470. AND (
  471. au.approvedBy IS NOT NULL
  472. OR au.createdBy = ''' + @user + '''
  473. OR ''Y'' = ''' + @hasRight + '''
  474. )
  475. ) '
  476. end
  477. END
  478. IF @flag = 'i'
  479. BEGIN
  480. IF NOT EXISTS(SELECT 'X' FROM agentMaster WITH(NOLOCK) WHERE agentId = @agentId AND ISNULL(isActive, 'N') = 'Y')
  481. BEGIN
  482. EXEC proc_errorHandler 1, 'Corresponding Agent has not been approved yet', @employeeId
  483. RETURN
  484. END
  485. IF EXISTS (SELECT 'X' FROM applicationUsers WHERE [userName] = @userName )
  486. BEGIN
  487. SET @msg = 'User Name ' + @userName + ' already exist'
  488. EXEC proc_errorHandler 1, @msg, @employeeId
  489. RETURN
  490. END
  491. BEGIN TRANSACTION
  492. INSERT INTO applicationUsers (
  493. [userName]
  494. ,agentCode
  495. ,firstName
  496. ,middleName
  497. ,lastName
  498. ,salutation
  499. ,gender
  500. ,countryId
  501. ,state
  502. ,district
  503. ,zip
  504. ,city
  505. ,[address]
  506. ,telephoneNo
  507. ,mobileNo
  508. ,email
  509. ,pwd
  510. ,agentId
  511. ,sessionTimeOutPeriod
  512. ,tranApproveLimit
  513. ,agentCrLimitAmt
  514. ,loginTime
  515. ,logoutTime
  516. ,userAccessLevel
  517. ,perDayTranLimit
  518. ,fromSendTrnTime
  519. ,toSendTrnTime
  520. ,fromPayTrnTime
  521. ,toPayTrnTime
  522. ,fromRptViewTime
  523. ,toRptViewTime
  524. ,isDeleted
  525. ,approvedDate
  526. ,lastLoginTs
  527. ,pwdChangeDays
  528. ,pwdChangeWarningDays
  529. ,lastPwdChangedOn
  530. ,forceChangePwd
  531. ,maxReportViewDays
  532. ,createdBy
  533. ,createdDate
  534. ,employeeId
  535. ,userType
  536. ,isActive
  537. ,txnPwd
  538. )
  539. SELECT
  540. @userName
  541. ,@agentCode
  542. ,@firstName
  543. ,@middleName
  544. ,@lastName
  545. ,@salutation
  546. ,@gender
  547. ,@countryId
  548. ,@state
  549. ,@district
  550. ,@zip
  551. ,@city
  552. ,@address
  553. ,@telephoneNo
  554. ,@mobileNo
  555. ,@email
  556. ,dbo.FNAEncryptString(@userName+'@123')
  557. ,@agentId
  558. ,@sessionTimeOutPeriod
  559. ,@tranApproveLimit
  560. ,@agentCrLimitAmt
  561. ,@loginTime
  562. ,@logoutTime
  563. ,@userAccessLevel
  564. ,@perDayTranLimit
  565. ,@fromSendTrnTime
  566. ,@toSendTrnTime
  567. ,@fromPayTrnTime
  568. ,@toPayTrnTime
  569. ,@fromRptViewTime
  570. ,@toRptViewTime
  571. ,@isDeleted
  572. ,@approvedDate
  573. ,@lastLoginTs
  574. ,@pwdChangeDays
  575. ,@pwdChangeWarningDays
  576. ,@lastPwdChangedOn
  577. ,'Y'
  578. ,@maxReportViewDays
  579. ,@user
  580. ,GETDATE()
  581. ,@employeeId
  582. ,@userType
  583. ,'Y'
  584. ,@txnPwd
  585. SET @userId = SCOPE_IDENTITY()
  586. IF @employeeId IS NULL
  587. BEGIN
  588. UPDATE applicationUsers SET
  589. employeeId = CAST(@userId AS VARCHAR)
  590. WHERE userId = @userId
  591. END
  592. IF @agentCode IS NULL
  593. BEGIN
  594. SELECT @agentCode = agentCode FROM agentMaster WITH(NOLOCK) WHERE agentId = @agentId
  595. UPDATE applicationUsers SET
  596. agentCode = @agentCode
  597. WHERE userId = @userId
  598. END
  599. ELSE IF @agentCode IS NOT NULL
  600. BEGIN
  601. UPDATE agentMaster SET agentCode = @agentCode WHERE agentId = @agentId
  602. END
  603. --Keep Password History--------------------------------------
  604. INSERT INTO passwordHistory(
  605. userName
  606. ,pwd
  607. ,createdDate
  608. )
  609. SELECT
  610. @userName
  611. ,dbo.FNAEncryptString(@userName+'@123')
  612. ,GETDATE()
  613. --------------------------------------------------------------
  614. COMMIT TRANSACTION
  615. SELECT 0 errorCode, 'Record has been added successfully with User Code ' + CAST(@employeeId AS VARCHAR) mes, @userId id
  616. END
  617. ELSE IF @flag = 'u'
  618. BEGIN
  619. IF EXISTS (SELECT 'X' FROM applicationUsers WITH(NOLOCK) WHERE userId = @userId AND approvedBy IS NULL AND createdBy <> @user)
  620. BEGIN
  621. EXEC proc_errorHandler 1, 'You can not modify this record. Previous Modification has not been approved yet.', @userId
  622. RETURN
  623. END
  624. IF EXISTS (SELECT 'X' FROM applicationUsersMod WITH(NOLOCK) WHERE userId = @userId AND createdBy <> @user)
  625. BEGIN
  626. EXEC proc_errorHandler 1, 'You can not modify this record. Previous Modification has not been approved yet.', @userId
  627. RETURN
  628. END
  629. IF EXISTS (SELECT 'X' FROM applicationUserRolesMod WITH(NOLOCK) WHERE userId = @userId AND createdBy <> @user)
  630. BEGIN
  631. EXEC proc_errorHandler 1, 'You can not modify this record. Previous Modification has not been approved yet.', @userId
  632. RETURN
  633. END
  634. IF EXISTS (SELECT 'X' FROM applicationUserFunctionsMod WITH(NOLOCK) WHERE userId = @userId AND createdBy <> @user)
  635. BEGIN
  636. EXEC proc_errorHandler 1, 'You can not modify this record. Previous Modification has not been approved yet.', @userId
  637. RETURN
  638. END
  639. BEGIN TRANSACTION
  640. SELECT @agentId = agentId FROM applicationUsers WITH(NOLOCK) WHERE userId = @userId
  641. IF EXISTS (SELECT 'X' FROM applicationUsers WITH(NOLOCK) WHERE userId = @userId AND approvedBy IS NULL AND createdBy = @user)
  642. BEGIN
  643. UPDATE applicationUsers SET
  644. firstName = @firstName
  645. ,middleName = @middleName
  646. ,lastName = @lastName
  647. ,salutation = @salutation
  648. ,gender = @gender
  649. ,telephoneNo = @telephoneNo
  650. ,mobileNo = @mobileNo
  651. ,state = @state
  652. ,district = @district
  653. ,zip = @zip
  654. ,[address] = @address
  655. ,city = @city
  656. ,countryId = @countryId
  657. ,email = @email
  658. ,agentId = @agentId
  659. ,sessionTimeOutPeriod = @sessionTimeOutPeriod
  660. ,tranApproveLimit = @tranApproveLimit
  661. ,agentCrLimitAmt = @agentCrLimitAmt
  662. ,loginTime = @loginTime
  663. ,logoutTime = @logoutTime
  664. ,userAccessLevel = @userAccessLevel
  665. ,perDayTranLimit = @perDayTranLimit
  666. ,fromSendTrnTime = @fromSendTrnTime
  667. ,toSendTrnTime = @toSendTrnTime
  668. ,fromPayTrnTime = @fromPayTrnTime
  669. ,toPayTrnTime = @toPayTrnTime
  670. ,fromRptViewTime = @fromRptViewTime
  671. ,toRptViewTime = @toRptViewTime
  672. ,pwdChangeDays = @pwdChangeDays
  673. ,pwdChangeWarningDays = @pwdChangeWarningDays
  674. ,maxReportViewDays = @maxReportViewDays
  675. ,userType = @userType
  676. ,isActive = @isActive
  677. WHERE userId = @userId
  678. END
  679. ELSE
  680. BEGIN
  681. DELETE FROM applicationUsersMod WHERE userId = @userId
  682. INSERT INTO applicationUsersMod (
  683. userId
  684. ,agentCode
  685. --,employeeId
  686. ,userName
  687. ,firstName
  688. ,middleName
  689. ,lastName
  690. ,salutation
  691. ,gender
  692. ,telephoneNo
  693. ,mobileNo
  694. ,state
  695. ,district
  696. ,zip
  697. ,[address]
  698. ,city
  699. ,countryId
  700. ,email
  701. ,agentId
  702. ,sessionTimeOutPeriod
  703. ,tranApproveLimit
  704. ,agentCrLimitAmt
  705. ,loginTime
  706. ,logoutTime
  707. ,userAccessLevel
  708. ,perDayTranLimit
  709. ,fromSendTrnTime
  710. ,toSendTrnTime
  711. ,fromPayTrnTime
  712. ,toPayTrnTime
  713. ,fromRptViewTime
  714. ,toRptViewTime
  715. ,pwdChangeDays
  716. ,pwdChangeWarningDays
  717. ,maxReportViewDays
  718. ,createdDate
  719. ,createdBy
  720. ,modType
  721. ,userType
  722. ,isActive
  723. )
  724. SELECT
  725. @userId
  726. ,@agentCode
  727. --,@employeeId
  728. ,@userName
  729. ,@firstName
  730. ,@middleName
  731. ,@lastName
  732. ,@salutation
  733. ,@gender
  734. ,@telephoneNo
  735. ,@mobileNo
  736. ,@state
  737. ,@district
  738. ,@zip
  739. ,@address
  740. ,@city
  741. ,@countryId
  742. ,@email
  743. ,@agentId
  744. ,@sessionTimeOutPeriod
  745. ,@tranApproveLimit
  746. ,@agentCrLimitAmt
  747. ,@loginTime
  748. ,@logoutTime
  749. ,@userAccessLevel
  750. ,@perDayTranLimit
  751. ,@fromSendTrnTime
  752. ,@toSendTrnTime
  753. ,@fromPayTrnTime
  754. ,@toPayTrnTime
  755. ,@fromRptViewTime
  756. ,@toRptViewTime
  757. ,@pwdChangeDays
  758. ,@pwdChangeWarningDays
  759. ,@maxReportViewDays
  760. ,GETDATE()
  761. ,@user
  762. ,'U'
  763. ,@userType
  764. ,@isActive
  765. END
  766. COMMIT TRANSACTION
  767. EXEC proc_errorHandler 0, 'Record updated successfully', @agentId
  768. END
  769. ELSE IF @flag = 'a'
  770. BEGIN
  771. IF EXISTS (SELECT 'X' FROM applicationUsersMod WITH(NOLOCK) WHERE userId = @userId AND createdBy = @user)
  772. BEGIN
  773. SELECT
  774. mode.*
  775. ,agentName = am.agentName + '|' + CAST(am.agentId AS VARCHAR) + '|' + CAST(am.agentType AS VARCHAR)
  776. ,dbo.FNADecryptString(mode.pwd) as DePWD
  777. ,main.modifiedBy
  778. ,main.modifiedDate
  779. ,dbo.FNADecryptString(mode.txnPwd) as DeTxnPWD
  780. FROM applicationUsersMod mode WITH(NOLOCK)
  781. INNER JOIN applicationUsers main WITH(NOLOCK) ON mode.userId = main.userId
  782. LEFT JOIN agentMaster am WITH(NOLOCK) ON mode.agentId = am.agentId
  783. WHERE mode.userId = @userId
  784. END
  785. ELSE
  786. BEGIN
  787. SELECT
  788. main.*
  789. ,agentName = am.agentName + '|' + CAST(am.agentId AS VARCHAR) + '|' + CAST(am.agentType AS VARCHAR)
  790. ,dbo.FNADecryptString(pwd) as DePWD
  791. ,dbo.FNADecryptString(txnPwd) as DeTxnPWD
  792. FROM applicationUsers main WITH(NOLOCK)
  793. LEFT JOIN agentMaster am WITH(NOLOCK) ON main.agentId = am.agentId
  794. WHERE main.userId = @userId
  795. END
  796. END
  797. ELSE IF @flag = 'a1'
  798. BEGIN
  799. SELECT
  800. main.*
  801. ,agentName = am.agentName + '|' + CAST(am.agentId AS VARCHAR) + '|' + CAST(am.agentType AS VARCHAR)
  802. ,dbo.FNADecryptString(pwd) as DePWD
  803. ,dbo.FNADecryptString(txnPwd) as DeTxnPWD
  804. FROM applicationUsers main WITH(NOLOCK)
  805. LEFT JOIN agentMaster am WITH(NOLOCK) ON main.agentId = am.agentId
  806. WHERE main.userId = @userId
  807. END
  808. ELSE IF @flag = 'pullDefault'
  809. BEGIN
  810. --SELECT top 1 main.* FROM applicationUsers main WITH(NOLOCK) WHERE agentId = @parentAgentId
  811. SELECT TOP 1
  812. city = agentCity
  813. ,countryId = agentCountryId
  814. ,[state] = agentState
  815. ,[district] = agentDistrict
  816. ,zip = agentZip
  817. ,[address] = agentAddress
  818. ,phone1 = agentPhone1
  819. ,phone2 = agentPhone2
  820. ,mobile1 = agentMobile1
  821. ,mobile2 = agentMobile2
  822. ,email = agentEmail1
  823. FROM agentMaster WITH(NOLOCK) WHERE agentId = @agentId
  824. --SELECT main.* FROM applicationUsers main WITH(NOLOCK) WHERE agentId = 1
  825. END
  826. ELSE IF @flag = 'd'
  827. BEGIN
  828. IF @userId = 1
  829. BEGIN
  830. EXEC proc_errorHandler 1, 'You can not delete admin user', @userId
  831. RETURN
  832. END
  833. IF EXISTS (SELECT 'X' FROM applicationUsers WITH(NOLOCK) WHERE userId = @userId AND approvedBy IS NULL AND createdBy <> @user)
  834. BEGIN
  835. EXEC proc_errorHandler 1, 'You can not delete this record. Previous Modification has not been approved yet.', @userId
  836. RETURN
  837. END
  838. IF EXISTS (SELECT 'X' FROM applicationUsersMod WITH(NOLOCK) WHERE userId = @userId AND createdBy <> @user)
  839. BEGIN
  840. EXEC proc_errorHandler 1, 'You can not delete this record. Previous Modification has not been approved yet.', @userId
  841. RETURN
  842. END
  843. IF EXISTS (SELECT 'X' FROM applicationUserRolesMod WITH(NOLOCK) WHERE userId = @userId AND createdBy <> @user)
  844. BEGIN
  845. EXEC proc_errorHandler 1, 'You can not delete this record. Previous Modification has not been approved yet.', @userId
  846. RETURN
  847. END
  848. IF EXISTS (SELECT 'X' FROM applicationUserFunctionsMod WITH(NOLOCK) WHERE userId = @userId AND createdBy <> @user)
  849. BEGIN
  850. EXEC proc_errorHandler 1, 'You can not delete this record. Previous Modification has not been approved yet.', @userId
  851. RETURN
  852. END
  853. BEGIN TRANSACTION
  854. IF EXISTS (SELECT 'X' FROM applicationUsers WITH(NOLOCK) WHERE userId = @userId AND approvedBy IS NULL AND createdBy = @user)
  855. BEGIN
  856. DELETE FROM applicationUsers WHERE userId = @userId
  857. END
  858. ELSE
  859. BEGIN
  860. INSERT INTO applicationUsersMod (
  861. userId
  862. ,userName
  863. ,agentCode
  864. ,firstName
  865. ,middleName
  866. ,lastName
  867. ,salutation
  868. ,gender
  869. ,telephoneNo
  870. ,mobileNo
  871. ,state
  872. ,district
  873. ,zip
  874. ,[address]
  875. ,city
  876. ,countryId
  877. ,email
  878. ,agentId
  879. ,sessionTimeOutPeriod
  880. ,tranApproveLimit
  881. ,agentCrLimitAmt
  882. ,loginTime
  883. ,logoutTime
  884. ,userAccessLevel
  885. ,perDayTranLimit
  886. ,fromSendTrnTime
  887. ,toSendTrnTime
  888. ,fromPayTrnTime
  889. ,toPayTrnTime
  890. ,fromRptViewTime
  891. ,toRptViewTime
  892. ,pwdChangeDays
  893. ,pwdChangeWarningDays
  894. ,maxReportViewDays
  895. ,createdDate
  896. ,createdBy
  897. ,modType
  898. ,userType
  899. ,isActive
  900. )
  901. SELECT
  902. userId
  903. ,userName
  904. ,agentCode
  905. ,firstName
  906. ,middleName
  907. ,lastName
  908. ,salutation
  909. ,gender
  910. ,telephoneNo
  911. ,mobileNo
  912. ,state
  913. ,district
  914. ,zip
  915. ,[address]
  916. ,city
  917. ,countryId
  918. ,email
  919. ,agentId
  920. ,sessionTimeOutPeriod
  921. ,tranApproveLimit
  922. ,agentCrLimitAmt
  923. ,loginTime
  924. ,logoutTime
  925. ,userAccessLevel
  926. ,perDayTranLimit
  927. ,fromSendTrnTime
  928. ,toSendTrnTime
  929. ,fromPayTrnTime
  930. ,toPayTrnTime
  931. ,fromRptViewTime
  932. ,toRptViewTime
  933. ,pwdChangeDays
  934. ,pwdChangeWarningDays
  935. ,@maxReportViewDays
  936. ,GETDATE()
  937. ,@user
  938. ,'D'
  939. ,userType
  940. ,isActive
  941. FROM applicationUsers WHERE userId = @userId
  942. END
  943. COMMIT TRANSACTION
  944. EXEC proc_errorHandler 0, 'Record deleted successfully', @userId
  945. END
  946. ELSE IF @flag = 'hs'
  947. BEGIN
  948. IF @sortBy IS NULL
  949. SET @sortBy = 'userId'
  950. IF @sortOrder IS NULL
  951. SET @sortOrder = 'ASC'
  952. SET @table = '(
  953. SELECT
  954. main.userId
  955. ,main.userName
  956. ,name = ISNULL(main.firstName, '''') + ISNULL( '' '' + main.middleName, '''')+ ISNULL( '' '' + main.lastName, '''')
  957. ,main.firstName
  958. ,main.middleName
  959. ,main.lastName
  960. ,main.address
  961. ,main.countryId
  962. ,countryName = cm.countryName
  963. ,main.agentId
  964. ,am.agentName
  965. ,main.agentCode
  966. ,main.isLocked
  967. ,main.lastLoginTs
  968. ,main.lastPwdChangedOn
  969. ,lockStatus = CASE WHEN ISNULL(main.isLocked, ''N'') = ''N'' THEN ''N | <a href="#" onclick="UnlockUser('' + CAST(main.userId AS VARCHAR) + '')">Lock</a>''
  970. WHEN ISNULL(main.isLocked, ''N'') = ''Y'' THEN ''Y | <a href="#" onclick="UnlockUser('' + CAST(main.userId AS VARCHAR) + '')">Unlock</a> | <a id="showSlab_'' + CAST(main.userId AS VARCHAR) + ''" href="#" onclick="ShowSlab('' + CAST(main.userId
  971. AS VARCHAR) + '','''''' + main.userName + '''''')">View Reason</a>'' END
  972. ,userGroup = CASE WHEN am.agentType = ''2904'' OR am.actAsBranch = ''Y'' THEN ''''
  973. ELSE
  974. ''<a href="/SwiftSystem/UserManagement/ApplicationUserSetup/UserGroupMaping.aspx?userName='' + main.userName + ''&userId='' + CAST(main.userId AS VARCHAR) + ''&agentId='' + CAST(main.agentId AS VARCHAR) + ''" ">
  975. <img src="/images/user_icon.gif" border=0 title="User Grouping" alt="User Group" /></a>'' END
  976. ,main.haschanged
  977. ,main.modifiedBy
  978. ,main.createdBy
  979. ,main.isActive
  980. FROM ' + @table + ' main
  981. INNER JOIN agentMaster am ON main.agentId = am.agentId
  982. LEFT JOIN countryMaster cm ON main.countryId = cm.countryId
  983. WHERE userName <> ''' + @user + ''' AND am.agentType = 2901
  984. ) x'
  985. SET @sql_filter = ''
  986. IF @haschanged IS NOT NULL
  987. SET @sql_filter = @sql_filter + ' AND haschanged = ''' + @haschanged + ''''
  988. IF @userName IS NOT NULL
  989. SET @sql_filter = @sql_filter + ' AND ISNULL(userName, '''') LIKE ''%' + @userName + '%'''
  990. IF @firstName IS NOT NULL
  991. SET @sql_filter = @sql_filter + ' AND ISNULL(firstName, '''') LIKE ''%' + @firstName + '%'''
  992. IF @lastName IS NOT NULL
  993. SET @sql_filter = @sql_filter + ' AND ISNULL(lastName, '''') LIKE ''%' + @lastName + '%'''
  994. IF @countryId IS NOT NULL
  995. SET @sql_filter = @sql_filter + ' AND ISNULL(countryId, '''') = ' + CAST(@countryId AS VARCHAR)
  996. IF @agentId IS NOT NULL
  997. SET @sql_filter = @sql_filter + ' AND ISNULL(agentId, '''') = ' + CAST(@agentId AS VARCHAR)
  998. IF @agentName IS NOT NULL
  999. SET @sql_filter = @sql_filter + ' AND ISNULL(agentName, '''') LIKE ''%' + @agentName + '%'''
  1000. IF @isLocked IS NOT NULL
  1001. SET @sql_filter = @sql_filter + ' AND ISNULL(isLocked, ''N'') = ''' + @isLocked + ''''
  1002. IF @isActive IS NOT NULL
  1003. SET @sql_filter = @sql_filter + ' AND ISNULL(isActive, ''Y'') = ''' + @isActive + ''''
  1004. SET @select_field_list ='
  1005. userId
  1006. ,userName
  1007. ,name
  1008. ,firstName
  1009. ,middleName
  1010. ,lastName
  1011. ,address
  1012. ,countryId
  1013. ,countryName
  1014. ,agentId
  1015. ,agentName
  1016. ,agentCode
  1017. ,isLocked
  1018. ,lockStatus
  1019. ,userGroup
  1020. ,haschanged
  1021. ,modifiedBy
  1022. ,createdBy
  1023. ,isActive
  1024. ,lastLoginTs
  1025. ,lastPwdChangedOn
  1026. '
  1027. EXEC dbo.proc_paging
  1028. @table
  1029. ,@sql_filter
  1030. ,@select_field_list
  1031. ,@extra_field_list
  1032. ,@sortBy
  1033. ,@sortOrder
  1034. ,@pageSize
  1035. ,@pageNumber
  1036. END
  1037. ELSE IF @flag = 's'
  1038. BEGIN
  1039. IF @sortBy IS NULL
  1040. SET @sortBy = 'userId'
  1041. IF @sortOrder IS NULL
  1042. SET @sortOrder = 'ASC'
  1043. SET @table = '(
  1044. SELECT
  1045. main.userId
  1046. ,main.userName
  1047. ,name = ISNULL(main.firstName, '''') + ISNULL( '' '' + main.middleName, '''')+ ISNULL( '' '' + main.lastName, '''')
  1048. ,main.firstName
  1049. ,main.middleName
  1050. ,main.lastName
  1051. ,main.address
  1052. ,contactNo = ISNULL(main.telephoneNo, main.mobileNo)
  1053. ,main.countryId
  1054. ,countryName = cm.countryName
  1055. ,main.agentId
  1056. ,am.agentName
  1057. ,main.agentCode
  1058. ,main.employeeId
  1059. ,main.isLocked
  1060. ,main.userType
  1061. ,lockStatus = CASE WHEN ISNULL(main.isLocked, ''N'') = ''N'' THEN ''N | <a href="#" onclick="UnlockUser('' + CAST(main.userId AS VARCHAR) + '')">Lock</a>''
  1062. WHEN ISNULL(main.isLocked, ''N'') = ''Y'' THEN ''Y | <a href="#" onclick="UnlockUser('' + CAST(main.userId AS VARCHAR) + '')">Unlock</a> | <a id="showSlab_'' + CAST(main.userId AS VARCHAR)
  1063. + ''" href="#" onclick="ShowSlab('' + CAST(main.userId AS VARCHAR) + '','''''' + main.userName + '''''')">View Reason</a>'' END
  1064. ,userGroup = CASE WHEN am.agentType = ''2904'' OR am.actAsBranch = ''Y'' THEN ''''
  1065. ELSE
  1066. ''<a href="/SwiftSystem/UserManagement/ApplicationUserSetup/UserGroupMaping.aspx?userName='' + main.userName + ''&userId='' + CAST(main.userId AS VARCHAR) + ''&agentId='' + CAST(main.agentId AS VARCHAR) + ''" ">
  1067. <img src="/images/user_icon.gif" border=0 title="User Grouping" alt="User Group" /></a>'' END
  1068. ,main.haschanged
  1069. ,main.modifiedBy
  1070. ,main.createdBy
  1071. ,main.isActive
  1072. ,main.lastLoginTs
  1073. ,main.lastPwdChangedOn
  1074. FROM ' + @table + ' main
  1075. INNER JOIN agentMaster am ON main.agentId = am.agentId
  1076. LEFT JOIN countryMaster cm ON main.countryId = cm.countryId
  1077. WHERE
  1078. userName <> ''' + @user + ''' AND am.agentType <> 2901
  1079. ) x'
  1080. SET @sql_filter = ''
  1081. IF @haschanged IS NOT NULL
  1082. SET @sql_filter = @sql_filter + ' AND haschanged = ''' + @haschanged + ''''
  1083. IF @userType IS NOT NULL
  1084. SET @sql_filter = @sql_filter + ' AND userType = ''' + @userType + ''''
  1085. IF @userName IS NOT NULL
  1086. SET @sql_filter = @sql_filter + ' AND ISNULL(userName, '''') LIKE ''' + @userName + '%'''
  1087. IF @firstName IS NOT NULL
  1088. SET @sql_filter = @sql_filter + ' AND ISNULL(firstName, '''') LIKE ''' + @firstName + '%'''
  1089. IF @lastName IS NOT NULL
  1090. SET @sql_filter = @sql_filter + ' AND ISNULL(lastName, '''') LIKE ''' + @lastName + '%'''
  1091. IF @countryId IS NOT NULL
  1092. SET @sql_filter = @sql_filter + ' AND ISNULL(countryId, '''') = ' + CAST(@countryId AS VARCHAR)
  1093. IF @agentId IS NOT NULL
  1094. SET @sql_filter = @sql_filter + ' AND ISNULL(agentId, '''') = ' + CAST(@agentId AS VARCHAR)
  1095. IF @agentName IS NOT NULL
  1096. SET @sql_filter = @sql_filter + ' AND ISNULL(agentName, '''') LIKE ''' + @agentName + '%'''
  1097. IF @isLocked IS NOT NULL
  1098. SET @sql_filter = @sql_filter + ' AND ISNULL(isLocked, ''N'') = ''' + @isLocked + ''''
  1099. IF @isActive IS NOT NULL
  1100. SET @sql_filter = @sql_filter + ' AND ISNULL(isActive, ''Y'') = ''' + @isActive + ''''
  1101. IF @countryName IS NOT NULL
  1102. SET @sql_filter = @sql_filter + ' AND ISNULL(countryName, '''') LIKE ''' + @countryName + '%'''
  1103. SET @select_field_list ='
  1104. userId
  1105. ,userName
  1106. ,userType
  1107. ,name
  1108. ,firstName
  1109. ,middleName
  1110. ,lastName
  1111. ,address
  1112. ,contactNo
  1113. ,countryId
  1114. ,countryName
  1115. ,agentId
  1116. ,agentName
  1117. ,agentCode
  1118. ,employeeId
  1119. ,isLocked
  1120. ,lockStatus
  1121. ,userGroup
  1122. ,haschanged
  1123. ,modifiedBy
  1124. ,createdBy
  1125. ,isActive
  1126. ,lastLoginTs
  1127. ,lastPwdChangedOn
  1128. '
  1129. EXEC dbo.proc_paging
  1130. @table
  1131. ,@sql_filter
  1132. ,@select_field_list
  1133. ,@extra_field_list
  1134. ,@sortBy
  1135. ,@sortOrder
  1136. ,@pageSize
  1137. ,@pageNumber
  1138. END
  1139. ELSE IF @flag = 'cps' --Check Password Status
  1140. BEGIN
  1141. SELECT forceChangePwd FROM applicationUsers WITH(NOLOCK) WHERE userName = @userName
  1142. END
  1143. ELSE IF @flag = 'cpcwd' --Check Password Change Warning Days
  1144. BEGIN
  1145. SELECT
  1146. @lastPwdChangedOn = ISNULL(lastPwdChangedOn, GETDATE())
  1147. ,@pwdChangeDays = pwdChangeDays
  1148. ,@pwdChangeWarningDays = pwdChangeWarningDays
  1149. FROM applicationUsers au WITH(NOLOCK) WHERE au.[userName] = @userName
  1150. DECLARE @pwdDays INT
  1151. SET @pwdDays = DATEDIFF(d, @lastPwdChangedOn, GETDATE())
  1152. IF @pwdDays >= @pwdChangeWarningDays
  1153. BEGIN
  1154. SELECT
  1155. '101' errorCode
  1156. ,'Your password will expire in ' + CAST(@pwdChangeDays - @pwdDays AS VARCHAR) + ' day(s). <a href="/SwiftSystem/UserManagement/ApplicationUserSetup/ChangePassword.aspx" target="frmame_main" >Change Password</a>' msg
  1157. ,NULL id
  1158. RETURN
  1159. END
  1160. ELSE
  1161. SELECT '0' errorCode, NULL msg, NULL id
  1162. END
  1163. ELSE IF @flag = 'cp'
  1164. BEGIN
  1165. IF NOT EXISTS (SELECT 'X' FROM applicationUsers au WITH(NOLOCK)
  1166. WHERE au.[userName] = @userName AND pwd = dbo.FNAEncryptString(@oldPwd))
  1167. BEGIN
  1168. SELECT 1 errorCode, 'Old password is not correct.' mes, @userName id
  1169. RETURN
  1170. END
  1171. DECLARE @pwdHistoryNum INT = NULL
  1172. DECLARE @tempPwdTable TABLE(pwd VARCHAR(50))
  1173. SELECT @pwdHistoryNum = pwdHistoryNum FROM passwordFormat WITH(NOLOCK)
  1174. SET @sql = 'SELECT TOP ' + CAST(@pwdHistoryNum AS VARCHAR) + ' pwd FROM passwordHistory WITH(NOLOCK) WHERE userName = ''' + @userName + ''' ORDER BY createdDate DESC'
  1175. INSERT INTO @tempPwdTable
  1176. EXEC(@sql)
  1177. IF dbo.FNAEncryptString(@pwd) IN (SELECT pwd FROM @tempPwdTable)
  1178. BEGIN
  1179. EXEC proc_errorHandler 1, 'Password has been already used previously. Please enter the new one.', @userName
  1180. RETURN
  1181. END
  1182. --Validate Password From Password Policy---------------------------------------------------------------
  1183. IF(SELECT TOP 1 errorCode FROM dbo.FNAValidatePassword(@pwd)) <> 0
  1184. BEGIN
  1185. SELECT * FROM dbo.FNAValidatePassword(@pwd)
  1186. RETURN
  1187. END
  1188. -------------------------------------------------------------------------------------------------------
  1189. UPDATE applicationUsers SET
  1190. pwd = dbo.FNAEncryptString(@pwd)
  1191. ,lastPwdChangedOn = GETDATE()
  1192. ,forceChangePwd = 'N'
  1193. WHERE [userName]= @userName
  1194. --Keep password History---------------------------------------------------------------------
  1195. INSERT INTO passwordHistory(
  1196. userName
  1197. ,pwd
  1198. ,createdDate
  1199. ,createdBy
  1200. )
  1201. SELECT @userName, dbo.FNAEncryptString(@pwd), GETDATE(),@userName
  1202. ---------------------------------------------------------------------------------------------
  1203. SELECT 0 errorCode, 'Password has been changed successfully.' mes, @userName id
  1204. END
  1205. ELSE IF @flag = 'loc'
  1206. BEGIN
  1207. UPDATE applicationUsers SET
  1208. isLocked = 'Y'
  1209. WHERE [userName]= @userName
  1210. INSERT INTO userLockHistory(userName, lockReason, createdBy, createdDate)
  1211. SELECT @userName, @lockReason, 'system', GETDATE()
  1212. SELECT 0 errorCode, 'Your account has been locked. Please, contact your administrator.' mes, @userName id
  1213. END
  1214. ELSE IF @flag = 'r'
  1215. BEGIN
  1216. IF NOT EXISTS(SELECT 'X' FROM applicationUsers WHERE userName = @userName)
  1217. BEGIN
  1218. SELECT 1 errorCode, 'User not found' mes, @userName id
  1219. RETURN
  1220. END
  1221. UPDATE applicationUsers SET
  1222. pwd = dbo.FNAEncryptString(@pwd)
  1223. ,forceChangePwd = 'Y'
  1224. WHERE [userName] = @userName
  1225. --Keep password History---------------------------------------------------------------------
  1226. INSERT INTO passwordHistory(
  1227. userName
  1228. ,pwd
  1229. ,createdDate
  1230. ,createdBy
  1231. )
  1232. SELECT @userName, dbo.FNAEncryptString(@pwd), GETDATE(),@user
  1233. ---------------------------------------------------------------------------------------------
  1234. SELECT 0 errorCode, 'Password has been reset successfully.' mes, @userName id
  1235. RETURN
  1236. END
  1237. ELSE IF @flag = 'l' --Login
  1238. BEGIN
  1239. DECLARE @UserData varchar(2000)
  1240. SET @UserData ='User:'+ @userName +', UserCode:'+ CAST(@userId as varchar(20))
  1241. SELECT TOP 1 @agentType = agentType, @actAsBranch = actAsBranch, @agentId = agentId
  1242. FROM agentMaster WITH(NOLOCK)
  1243. WHERE agentId = (
  1244. SELECT agentId FROM applicationUsers WITH(NOLOCK) WHERE userName = @userName
  1245. )
  1246. IF (@agentType = (2904) OR @actAsBranch = 'Y')
  1247. BEGIN
  1248. SET @UserInfoDetail = 'Reason = Login fails, Incorrect user name.-:::-'+@UserInfoDetail
  1249. SELECT 1 errorCode, 'Login fails, Agent Cannot Login from here.' mes, @userName id
  1250. EXEC proc_applicationLogs
  1251. @flag='login',
  1252. @logType='Login fails',
  1253. @createdBy = @userName,
  1254. @Reason='Invalid Username',
  1255. @UserData = @UserData,
  1256. @fieldValue = @UserInfoDetail,
  1257. @agentId=@agentId
  1258. RETURN
  1259. END
  1260. IF NOT EXISTS(SELECT 'X' FROM applicationUsers WHERE [userName] = @userName AND ISNULL(isActive, 'N') = 'Y')
  1261. BEGIN
  1262. SET @UserInfoDetail = 'Reason = Login fails, User Locked .-:::-'+@UserInfoDetail
  1263. SELECT 1 errorCode, 'Login fails, Incorrect user name or password.' mes, @userName id
  1264. EXEC proc_applicationLogs
  1265. @flag='login',
  1266. @logType='Login fails',
  1267. @createdBy = @userName,
  1268. @Reason='User Not Actived',
  1269. @UserData = @UserData,
  1270. @fieldValue = @UserInfoDetail,
  1271. @agentId=@agentId
  1272. RETURN
  1273. END
  1274. IF NOT EXISTS(SELECT 'X' FROM applicationUsers WHERE [userName] = @userName AND pwd = dbo.FNAEncryptString(@pwd) AND userId = @userId AND ISNULL(isActive, 'N') = 'Y')
  1275. BEGIN
  1276. SELECT 2 errorCode, 'Login fails, Incorrect user name or password.' mes, @userName id
  1277. SET @UserInfoDetail = 'Reason = Login fails, Invalid password.-:::-'+@UserInfoDetail
  1278. EXEC proc_applicationLogs
  1279. @flag='login',
  1280. @logType='Login fails',
  1281. @createdBy = @userName,
  1282. @Reason='Invalid Password',
  1283. @UserData = @UserData,
  1284. @fieldValue = @UserInfoDetail,
  1285. @agentId=@agentId
  1286. RETURN
  1287. END
  1288. IF EXISTS(SELECT 'X' FROM applicationUsers WHERE [userName] = @userName AND userId = @userId AND pwd = dbo.FNAEncryptString(@pwd) AND ISNULL(isLocked, 'N') = 'Y')
  1289. BEGIN
  1290. SELECT 1 errorCode, 'Your account has been locked. Please, contact your administrator.' mes, @userName id
  1291. SET @UserInfoDetail = 'Reason = Login fails, Your account has been locked. Please, contact your administrator.-:::-'+@UserInfoDetail
  1292. EXEC proc_applicationLogs
  1293. @flag='login',
  1294. @logType='Login fails',
  1295. @createdBy = @userName,
  1296. @Reason='User Locked ',
  1297. @UserData = @UserData,
  1298. @fieldValue = @UserInfoDetail,
  1299. @agentId=@agentId
  1300. RETURN
  1301. END
  1302. IF NOT EXISTS(SELECT 'X' FROM applicationUsers WHERE userName = @userName AND pwd = dbo.FNAEncryptString(@pwd) AND userId = @userId AND ISNULL(isDeleted, 'N') <> 'Y' AND CAST(GETDATE() AS TIME) > loginTime AND CAST(GETDATE() AS TIME) < logoutTime)
  1303. BEGIN
  1304. SELECT 1 errorCode, 'You are not permitted to login at this time. Please, contact your administrator' mes, @userName id
  1305. SET @UserInfoDetail = 'Reason = You are not permitted to login at this time. Please, contact your administrator.-:::-'+@UserInfoDetail
  1306. EXEC proc_applicationLogs
  1307. @flag='login',
  1308. @logType='Login fails',
  1309. @createdBy = @userName,
  1310. @Reason='Login time Exeeded ',
  1311. @UserData = @UserData,
  1312. @fieldValue = @UserInfoDetail,
  1313. @agentId=@agentId
  1314. RETURN
  1315. END
  1316. -- DATE to DATE Lock setting
  1317. IF EXISTS(SELECT 'X' FROM userLockDetail
  1318. WHERE userId=@userId AND GETDATE() BETWEEN startDate AND endDate
  1319. AND ISNULL(isDeleted, 'N') = 'N'
  1320. )
  1321. BEGIN
  1322. SELECT 1 errorCode, 'You account is locked in this period. Please, contact your administrator' mes, @userName id
  1323. SET @UserInfoDetail = 'Reason = You account is locked in this period. Please, contact your administrator.-:::-'+@UserInfoDetail
  1324. EXEC proc_applicationLogs
  1325. @flag='login',
  1326. @logType='Login fails',
  1327. @createdBy = @userName,
  1328. @Reason='Not permitted to login in this period ',
  1329. @UserData = @UserData,
  1330. @fieldValue = @UserInfoDetail,
  1331. @agentId=@agentId
  1332. RETURN
  1333. END
  1334. -- Last Login date check for Locking
  1335. IF EXISTS(select 'X' from applicationUsers
  1336. where userId =@userId and
  1337. datediff (DAY,lastLoginTs,GETDATE())>=
  1338. (select top 1 isnull(lockUserDays,30) from passwordFormat
  1339. where isnull(isActive,'N')='Y')
  1340. )
  1341. BEGIN
  1342. update applicationUsers set
  1343. isLocked='Y'
  1344. ,lastLoginTs=getdate()
  1345. where userId = @userId
  1346. SELECT 1 errorCode, 'You are locked this time. Please, contact your administrator' mes, @userName id
  1347. SET @UserInfoDetail = 'Reason = You are locked this time. Please, contact your administrator.-:::-'+@UserInfoDetail
  1348. EXEC proc_applicationLogs
  1349. @flag='login',
  1350. @logType='Login fails',
  1351. @createdBy = @userName,
  1352. @Reason='Not Login for fix period, now user is locked',
  1353. @UserData = @UserData,
  1354. @fieldValue = @UserInfoDetail,
  1355. @agentId=@agentId
  1356. INSERT INTO userLockHistory(username, lockReason, createdBy, createdDate)
  1357. SELECT @userName, 'Your account has been locked due to not login for fix period', 'system', GETDATE()
  1358. RETURN;
  1359. END
  1360. IF EXISTS(select top 1 'y' from userLockDetail
  1361. where userId =@userId and GETDATE() between startDate
  1362. and convert(varchar(20), endDate,101) +' 23:59:59'
  1363. and isnull(isDeleted,'N')='N'
  1364. )
  1365. BEGIN
  1366. SELECT 1 errorCode, 'You are not permitted to login for this date. Please, contact your administrator' mes, @userName id
  1367. SET @UserInfoDetail = 'Reason = You are not permitted to login at this time. Please, contact your administrator-:::-'+@UserInfoDetail
  1368. EXEC proc_applicationLogs
  1369. @flag='login',
  1370. @logType='Login fails',
  1371. @createdBy = @userName,
  1372. @Reason='Not permitted to login for this date',
  1373. @UserData = @UserData,
  1374. @fieldValue = @UserInfoDetail,
  1375. @agentId=@agentId
  1376. RETURN;
  1377. END
  1378. BEGIN TRANSACTION
  1379. SELECT
  1380. 0 errorCode
  1381. ,REPLACE(ISNULL(au.firstName, '') + ISNULL(' ' + au.middleName, '') + ISNULL(' ' + au.lastName, ''), ' ', ' ') mes
  1382. ,id = CAST(au.userId AS VARCHAR) + '|' + @userName + '|' + CAST(ISNULL(lastLoginTs, GETDATE()) AS VARCHAR)
  1383. FROM applicationUsers au WITH(NOLOCK)
  1384. WHERE au.[userName] = @userName AND au.pwd = dbo.FNAEncryptString(@pwd)
  1385. SELECT
  1386. @lastPwdChangedOn = ISNULL(lastPwdChangedOn, GETDATE())
  1387. ,@forceChangePwd = ISNULL(forceChangePwd, 'N')
  1388. ,@pwdChangeDays = pwdChangeDays
  1389. ,@pwdChangeWarningDays = pwdChangeWarningDays
  1390. FROM applicationUsers au WITH(NOLOCK) WHERE au.[userName] = @userName AND au.pwd = dbo.FNAEncryptString(@pwd)
  1391. UPDATE applicationUsers SET
  1392. lastLoginTs = GETDATE()
  1393. WHERE [userName]= @userName
  1394. IF @@TRANCOUNT > 0
  1395. COMMIT TRANSACTION
  1396. IF @forceChangePwd = 'Y'
  1397. BEGIN
  1398. SELECT '100' errorCode, 'You are required to change your password.' msg , null id
  1399. SET @UserInfoDetail = 'Reason = You are required to change your password.-:::-'+@UserInfoDetail
  1400. EXEC proc_applicationLogs
  1401. @flag='login',
  1402. @logType='Login',
  1403. @createdBy = @userName,
  1404. @Reason='Admin Login',
  1405. @UserData = @UserData,
  1406. @fieldValue = @UserInfoDetail,
  1407. @agentId=@agentId
  1408. RETURN
  1409. END
  1410. DECLARE @password_days INT
  1411. SET @password_days = DATEDIFF(d, @lastPwdChangedOn, GETDATE())
  1412. IF @password_days >= @pwdChangeDays
  1413. BEGIN
  1414. SET @msg = 'Your password has expired. <a href="/SwiftSystem/UserManagement/ApplicationUserSetup/ChangePassword.aspx?userName=' + @userName + '&mode=admin" target="frmame_main" >Change Password</a>'
  1415. SELECT '101' errorCode, @msg msg , null id
  1416. SET @UserInfoDetail = 'Reason = Your password has expired.-:::-'+@UserInfoDetail
  1417. EXEC proc_applicationLogs
  1418. @flag='login',
  1419. @logType='Login fails',
  1420. @createdBy = @userName,
  1421. @Reason='Password expired',
  1422. @UserData = @UserData,
  1423. @fieldValue = @UserInfoDetail,
  1424. @agentId=@agentId
  1425. RETURN
  1426. END
  1427. IF @password_days >= @pwdChangeWarningDays
  1428. BEGIN
  1429. SELECT '102' errorCode, 'Your password will expire in' + CAST(@pwdChangeDays - @password_days AS VARCHAR) + ' day(s).' msg , null id
  1430. SET @UserInfoDetail = 'Reason = Your password will expire in' + CAST(@pwdChangeDays - @password_days AS VARCHAR) + ' day(s).-:::-'+@UserInfoDetail
  1431. EXEC proc_applicationLogs
  1432. @flag='login',
  1433. @logType='Login',
  1434. @createdBy = @userName,
  1435. @Reason='Admin Login',
  1436. @UserData = @UserData,
  1437. @fieldValue = @UserInfoDetail,
  1438. @agentId=@agentId
  1439. RETURN
  1440. END
  1441. SELECT 0 errorCode, 'Login success.' mes, @userName id
  1442. --Audit data starts
  1443. EXEC proc_applicationLogs
  1444. @flag='login',
  1445. @logType='Login',
  1446. @createdBy = @userName,
  1447. @Reason='Admin Login',
  1448. @UserData = @UserData,
  1449. @fieldValue = @UserInfoDetail,
  1450. @agentId=@agentId
  1451. --Audit data ends
  1452. END
  1453. ELSE IF @flag = 'lfa' --Login for Agent
  1454. BEGIN
  1455. set @UserData ='User:'+ @userName +', UserCode:'+ cast(@userId as varchar(20))
  1456. +', AgentCode:'+ cast(@agentCode as varchar(20))
  1457. SELECT top 1 @agentType = agentType, @actAsBranch = actAsBranch, @agentId = agentId
  1458. FROM agentMaster WITH(NOLOCK)
  1459. WHERE agentId = (
  1460. SELECT agentId FROM applicationUsers WITH(NOLOCK) WHERE userName = @userName
  1461. )
  1462. IF NOT EXISTS(SELECT 'X' FROM applicationUsers WHERE [userName] = @userName)
  1463. BEGIN
  1464. SELECT 1 errorCode, 'Login fails, Incorrect user name or password.' mes, @userName id
  1465. SET @UserInfoDetail = 'Reason = Login fails, Incorrect user name.-:::-'+@UserInfoDetail
  1466. EXEC proc_applicationLogs
  1467. @flag='login',
  1468. @logType='Login fails',
  1469. @createdBy = @userName,
  1470. @Reason='Invalid Username',
  1471. @UserData = @UserData,
  1472. @fieldValue = @UserInfoDetail,
  1473. @agentId=@agentId
  1474. RETURN
  1475. END
  1476. IF EXISTS(SELECT 'X' FROM applicationUsers WHERE userName = @userName AND pwd = dbo.FNAEncryptString(@pwd) AND agentCode = @agentCode AND userId = @userId AND ISNULL(isActive, 'N') = 'N')
  1477. BEGIN
  1478. SELECT 1 errorCode, 'User has not been approved.' mes, @userName id
  1479. SET @UserInfoDetail = 'Reason = User has not been approved.-:::-'+@UserInfoDetail
  1480. EXEC proc_applicationLogs
  1481. @flag='login',
  1482. @logType='Login fails',
  1483. @createdBy = @userName,
  1484. @Reason='User has not been approved',
  1485. @UserData = @UserData,
  1486. @fieldValue = @UserInfoDetail,
  1487. @agentId=@agentId
  1488. RETURN
  1489. END
  1490. IF NOT EXISTS(SELECT 'X' FROM applicationUsers WHERE [userName] = @userName
  1491. AND ISNULL(isActive, 'N') = 'Y')
  1492. BEGIN
  1493. SELECT 1 errorCode, 'Login fails, Incorrect user name or password.' mes, @userName id
  1494. SET @UserInfoDetail = 'Reason = Login fails, User is not Active.-:::-'+@UserInfoDetail
  1495. EXEC proc_applicationLogs
  1496. @flag='login',
  1497. @logType='Login fails',
  1498. @createdBy = @userName,
  1499. @Reason='User is not Active',
  1500. @UserData = @UserData,
  1501. @fieldValue = @UserInfoDetail,
  1502. @agentId=@agentId
  1503. RETURN
  1504. END
  1505. IF NOT EXISTS(SELECT 'X' FROM applicationUsers WHERE [userName] = @userName
  1506. AND pwd = dbo.FNAEncryptString(@pwd) and ISNULL(isActive, 'N') = 'Y')
  1507. BEGIN
  1508. SELECT 2 errorCode, 'Login fails, Incorrect user name or password.' mes, @userName id
  1509. SET @UserInfoDetail = 'Reason = Login fails, Incorrect password.-:::-'+@UserInfoDetail
  1510. EXEC proc_applicationLogs
  1511. @flag='login',
  1512. @logType='Login fails',
  1513. @createdBy = @userName,
  1514. @Reason='Incorrect password',
  1515. @UserData = @UserData,
  1516. @fieldValue = @UserInfoDetail,
  1517. @agentId=@agentId
  1518. RETURN
  1519. END
  1520. IF NOT EXISTS(SELECT 'X' FROM applicationUsers WHERE [userName] = @userName
  1521. AND pwd = dbo.FNAEncryptString(@pwd) and ISNULL(isActive, 'N') = 'Y'
  1522. AND agentCode = @agentCode)
  1523. BEGIN
  1524. SELECT 2 errorCode, 'Login fails, Incorrect user name or password.' mes, @userName id
  1525. SET @UserInfoDetail = 'Reason = Login fails, Incorrect AgentCode.-:::-'+@UserInfoDetail
  1526. EXEC proc_applicationLogs
  1527. @flag='login',
  1528. @logType='Login fails',
  1529. @createdBy = @userName,
  1530. @Reason='Incorrect AgentCode',
  1531. @UserData = @UserData,
  1532. @fieldValue = @UserInfoDetail,
  1533. @agentId=@agentId
  1534. RETURN
  1535. END
  1536. IF NOT EXISTS(SELECT 'X' FROM applicationUsers WHERE [userName] = @userName
  1537. AND pwd = dbo.FNAEncryptString(@pwd) and ISNULL(isActive, 'N') = 'Y'
  1538. AND agentCode = @agentCode AND userId = @userId )
  1539. BEGIN
  1540. SELECT 2 errorCode, 'Login fails, Incorrect user name or password.' mes, @userName id
  1541. SET @UserInfoDetail = 'Reason = Login fails, Incorrect userId.-:::-'+@UserInfoDetail
  1542. EXEC proc_applicationLogs
  1543. @flag='login',
  1544. @logType='Login fails',
  1545. @createdBy = @userName,
  1546. @Reason='Incorrect userId',
  1547. @UserData = @UserData,
  1548. @fieldValue = @UserInfoDetail,
  1549. @agentId=@agentId
  1550. RETURN
  1551. END
  1552. IF EXISTS(SELECT 'X' FROM applicationUsers WHERE [userName] = @userName
  1553. AND pwd = dbo.FNAEncryptString(@pwd) AND agentCode = @agentCode
  1554. AND userId = @userId AND ISNULL(isLocked, 'N') = 'Y')
  1555. BEGIN
  1556. SELECT 1 errorCode, 'Your account has been locked. Please, contact your administrator.' mes, @userName id
  1557. SET @UserInfoDetail = 'Reason = Your account has been locked. Please, contact your administrator.-:::-'+@UserInfoDetail
  1558. EXEC proc_applicationLogs
  1559. @flag='login',
  1560. @logType='Login fails',
  1561. @createdBy = @userName,
  1562. @Reason='Your account has been locked',
  1563. @UserData = @UserData,
  1564. @fieldValue = @UserInfoDetail,
  1565. @agentId=@agentId
  1566. RETURN;
  1567. END
  1568. IF NOT EXISTS(SELECT 'X' FROM applicationUsers WHERE userName = @userName
  1569. AND pwd = dbo.FNAEncryptString(@pwd) AND agentCode = @agentCode
  1570. AND userId = @userId AND ISNULL(isDeleted, 'N') <> 'Y'
  1571. AND CAST(GETDATE() AS TIME) > loginTime AND CAST(GETDATE() AS TIME) < logoutTime)
  1572. BEGIN
  1573. SELECT 1 errorCode, 'You are not permitted to login at this time. Please, contact your administrator' mes, @userName id
  1574. SET @UserInfoDetail = 'Reason = You are not permitted to login at this time. Please, contact your administrator-:::-'+@UserInfoDetail
  1575. EXEC proc_applicationLogs
  1576. @flag='login',
  1577. @logType='Login fails',
  1578. @createdBy = @userName,
  1579. @Reason='Not permitted to login at this time',
  1580. @UserData = @UserData,
  1581. @fieldValue = @UserInfoDetail,
  1582. @agentId=@agentId
  1583. RETURN
  1584. END
  1585. IF EXISTS(select top 1 'y' from userLockDetail
  1586. where userId =@userId and GETDATE() between startDate
  1587. and convert(varchar(20), endDate,101) +' 23:59:59'
  1588. and isnull(isDeleted,'N')='N')
  1589. BEGIN
  1590. SELECT 1 errorCode, 'You are not permitted to login for this date. Please, contact your administrator' mes, @userName id
  1591. SET @UserInfoDetail = 'Reason = You are not permitted to login at this time. Please, contact your administrator-:::-'+@UserInfoDetail
  1592. EXEC proc_applicationLogs
  1593. @flag='login',
  1594. @logType='Login fails',
  1595. @createdBy = @userName,
  1596. @Reason='Not permitted to login for this date',
  1597. @UserData = @UserData,
  1598. @fieldValue = @UserInfoDetail,
  1599. @agentId=@agentId
  1600. RETURN
  1601. END
  1602. -- Last Login date check for Locking
  1603. IF EXISTS(select 'X' from applicationUsers
  1604. where userId =@userId and
  1605. datediff (DAY,lastLoginTs,GETDATE())>=
  1606. (select top 1 isnull(lockUserDays,30) from passwordFormat
  1607. where isnull(isActive,'N')='Y')
  1608. )
  1609. BEGIN
  1610. update applicationUsers set
  1611. isLocked='Y'
  1612. ,lastLoginTs=getdate()
  1613. where userId = @userId
  1614. SELECT 1 errorCode, 'You are locked this time. Please, contact your administrator' mes, @userName id
  1615. SET @UserInfoDetail = 'Reason = You are locked this time. Please, contact your administrator.-:::-'+@UserInfoDetail
  1616. EXEC proc_applicationLogs
  1617. @flag='login',
  1618. @logType='Login fails',
  1619. @createdBy = @userName,
  1620. @Reason='Not Login for fix period, now user is locked',
  1621. @UserData = @UserData,
  1622. @fieldValue = @UserInfoDetail,
  1623. @agentId=@agentId
  1624. INSERT INTO userLockHistory(username, lockReason, createdBy, createdDate)
  1625. SELECT @userName, 'Your account has been locked due to not login for fix period', 'system', GETDATE()
  1626. RETURN;
  1627. END
  1628. IF EXISTS(select top 1 'y' from userLockDetail
  1629. where userId =@userId and GETDATE() between startDate
  1630. and convert(varchar(20), endDate,101) +' 23:59:59'
  1631. and isnull(isDeleted,'N')='N'
  1632. )
  1633. BEGIN
  1634. SELECT 1 errorCode, 'You are not permitted to login for this date. Please, contact your administrator' mes, @userName id
  1635. SET @UserInfoDetail = 'Reason = You are not permitted to login at this time. Please, contact your administrator-:::-'+@UserInfoDetail
  1636. EXEC proc_applicationLogs
  1637. @flag='login',
  1638. @logType='Login fails',
  1639. @createdBy = @userName,
  1640. @Reason='Not permitted to login for this date',
  1641. @UserData = @UserData,
  1642. @fieldValue = @UserInfoDetail,
  1643. @agentId=@agentId
  1644. RETURN;
  1645. END
  1646. BEGIN TRANSACTION
  1647. SELECT
  1648. 0 errorCode
  1649. ,REPLACE(ISNULL(au.firstName, '') + ISNULL(' ' + au.middleName, '') + ISNULL(' ' + au.lastName, ''), ' ', ' ') mes
  1650. ,id = CAST(au.userId AS VARCHAR) + '|' + @userName + '|' + CAST(ISNULL(lastLoginTs, GETDATE()) AS VARCHAR)
  1651. FROM applicationUsers au WITH(NOLOCK)
  1652. WHERE au.[userName] = @userName AND au.pwd = dbo.FNAEncryptString(@pwd)
  1653. SELECT
  1654. @lastPwdChangedOn = ISNULL(lastPwdChangedOn, GETDATE())
  1655. ,@forceChangePwd = ISNULL(forceChangePwd, 'N')
  1656. ,@pwdChangeDays = pwdChangeDays
  1657. ,@pwdChangeWarningDays = pwdChangeWarningDays
  1658. FROM applicationUsers au WITH(NOLOCK) WHERE au.[userName] = @userName AND au.pwd = dbo.FNAEncryptString(@pwd)
  1659. UPDATE applicationUsers SET
  1660. lastLoginTs = GETDATE()
  1661. WHERE [userName]= @userName
  1662. IF @@TRANCOUNT > 0
  1663. COMMIT TRANSACTION
  1664. IF @forceChangePwd = 'Y'
  1665. BEGIN
  1666. SELECT '100' errorCode, 'You are required to change your password.' msg , null id
  1667. SET @UserInfoDetail = 'Reason = You are required to change your password.-:::-'+@UserInfoDetail
  1668. EXEC proc_applicationLogs
  1669. @flag = 'login',
  1670. @logType = 'Login',
  1671. @createdBy = @userName,
  1672. @Reason = 'Agent Login',
  1673. @UserData = @UserData,
  1674. @fieldValue = @UserInfoDetail,
  1675. @agentId=@agentId
  1676. RETURN
  1677. END
  1678. SET @password_days = DATEDIFF(d, @lastPwdChangedOn, GETDATE())
  1679. IF @password_days >= @pwdChangeDays
  1680. BEGIN
  1681. SET @msg = 'Your password has expired. <a href="/SwiftSystem/UserManagement/ApplicationUserSetup/ChangePassword.aspx?userName=' + @userName + '&mode=agent" target="frmame_main" >Change Password</a>'
  1682. SELECT '101' errorCode, @msg msg , null id
  1683. SET @UserInfoDetail = 'Reason = You password has expired.-:::-'+@UserInfoDetail
  1684. EXEC proc_applicationLogs
  1685. @flag = 'login',
  1686. @logType = 'Login fails',
  1687. @createdBy = @userName,
  1688. @Reason = 'Password Expired',
  1689. @UserData = @UserData,
  1690. @fieldValue = @UserInfoDetail,
  1691. @agentId=@agentId
  1692. RETURN
  1693. END
  1694. IF @password_days >= @pwdChangeWarningDays
  1695. BEGIN
  1696. SELECT '102' errorCode, 'Your password will expire in' + CAST(@pwdChangeDays - @password_days AS VARCHAR) + ' day(s).' msg , null id
  1697. SET @UserInfoDetail = 'Reason = Your password will expire in' + CAST(@pwdChangeDays - @password_days AS VARCHAR) + ' day(s).-:::-'+@UserInfoDetail
  1698. EXEC proc_applicationLogs
  1699. @flag = 'login',
  1700. @logType = 'Login',
  1701. @createdBy = @userName,
  1702. @Reason = 'Agent Login',
  1703. @UserData = @UserData,
  1704. @fieldValue = @UserInfoDetail,
  1705. @agentId=@agentId
  1706. RETURN
  1707. END
  1708. SELECT 0 errorCode, 'Login success.' mes, @userName id
  1709. --Audit data starts
  1710. EXEC proc_applicationLogs
  1711. @flag='login',
  1712. @logType='Login',
  1713. @createdBy = @userName,
  1714. @Reason='Agent Login',
  1715. @UserData = @UserData,
  1716. @fieldValue = @UserInfoDetail,
  1717. @agentId=@agentId
  1718. --Audit data ends
  1719. END
  1720. ELSE IF @flag = 'cpe' ---Check Password Expiry
  1721. BEGIN
  1722. SELECT
  1723. @lastPwdChangedOn = ISNULL(lastPwdChangedOn, GETDATE())
  1724. ,@pwdChangeDays = pwdChangeDays
  1725. FROM applicationUsers au WITH(NOLOCK) WHERE au.[userName] = @userName
  1726. SET @password_days = DATEDIFF(d, @lastPwdChangedOn, GETDATE())
  1727. IF @password_days >= @pwdChangeDays
  1728. SELECT 'Y'
  1729. ELSE
  1730. SELECT 'N'
  1731. END
  1732. ELSE IF @flag = 'lockUser'
  1733. BEGIN
  1734. SELECT @isActive = ISNULL(isLocked,'N') FROM applicationUsers WITH(NOLOCK) WHERE userId = @userId
  1735. UPDATE applicationUsers
  1736. SET isLocked = CASE @isActive WHEN 'Y' THEN 'N' ELSE 'Y' END ,modifiedBy = @user,modifiedDate = GETDATE()
  1737. ,isActive = CASE @isActive WHEN 'Y' THEN 'Y' ELSE 'N' END
  1738. ,invalidAttemptCount = CASE WHEN @isActive='Y' THEN 0 ELSE invalidAttemptCount end
  1739. WHERE userId = @userId
  1740. SELECT @sql_filter = CASE WHEN @isActive = 'Y' THEN 'User unlocked Successfully' ELSE 'User locked Successfully' END
  1741. EXEC proc_errorHandler '0',@sql_filter,@userId
  1742. RETURN;
  1743. END
  1744. ELSE IF @flag = 'rdu'
  1745. BEGIN
  1746. UPDATE applicationUsers SET
  1747. isDeleted = 'N',
  1748. modifiedBy = @user,
  1749. modifiedDate = GETDATE()
  1750. WHERE userId = @userId
  1751. EXEC proc_errorHandler '0','User restored successfully',@userName
  1752. RETURN
  1753. END
  1754. ELSE IF @flag = 'cu' --check user
  1755. BEGIN
  1756. IF EXISTS(SELECT 'X' FROM applicationUsers WHERE [userName] = @userName AND ISNULL(isActive, 'N') <> 'Y')
  1757. BEGIN
  1758. SELECT 1 errorCode, 'Access is denied.' mes, @userName id
  1759. SET @UserInfoDetail = 'Access is denied-:::-'+@UserInfoDetail
  1760. EXEC proc_applicationLogs 'i', NULL, 'Login fails', 'Login fails', @userName, @userName,'', @UserInfoDetail, @module
  1761. RETURN
  1762. END
  1763. IF EXISTS(SELECT 'X' FROM applicationUsers WHERE [userName] = @userName AND ISNULL(isLocked, 'N') = 'Y')
  1764. BEGIN
  1765. SELECT 1 errorCode, 'Your account has been locked. Please, contact your administrator.' mes, @userName id
  1766. SET @UserInfoDetail = 'Your account has been locked. Please, contact your administrator.-:::-'+@UserInfoDetail
  1767. EXEC proc_applicationLogs 'i', NULL, 'Login fails', 'Login fails', @userName, @userName,'', @UserInfoDetail, @module
  1768. RETURN
  1769. END
  1770. BEGIN TRANSACTION
  1771. SELECT
  1772. 0 errorCode
  1773. ,REPLACE(ISNULL(au.firstName, '') + ' ' + ISNULL(au.middleName, '') + ' ' + ISNULL(au.lastName, ''), ' ', ' ') mes
  1774. ,@userName id
  1775. FROM applicationUsers au WITH(NOLOCK)
  1776. WHERE au.[userName] = @userName
  1777. IF @@TRANCOUNT > 0
  1778. COMMIT TRANSACTION
  1779. END
  1780. ELSE IF @flag = 'lo' --Log Out
  1781. BEGIN
  1782. BEGIN TRANSACTION
  1783. SELECT
  1784. 0 errorCode
  1785. ,REPLACE(ISNULL(au.firstName, '') + ' ' + ISNULL(au.middleName, '') + ' ' + ISNULL(au.lastName, ''), ' ', ' ') mes
  1786. ,@userName id
  1787. FROM applicationUsers au WITH(NOLOCK)
  1788. WHERE au.[userName] = @userName
  1789. EXEC proc_applicationLogs
  1790. @flag='login',
  1791. @logType='Logout',
  1792. @createdBy = @userName,
  1793. @Reason='Logout',
  1794. @UserData = @UserData,
  1795. @agentId=@agentId
  1796. --Audit data ends
  1797. IF @@TRANCOUNT > 0
  1798. COMMIT TRANSACTION
  1799. END
  1800. ELSE IF @flag = 'reject'
  1801. BEGIN
  1802. IF NOT EXISTS (SELECT 'X' FROM applicationUsers WITH(NOLOCK) WHERE userId = @userId AND approvedBy IS NULL)
  1803. AND
  1804. NOT EXISTS(SELECT 'X' FROM applicationUsersMod WITH(NOLOCK) WHERE userId = @userId )
  1805. BEGIN
  1806. EXEC proc_errorHandler 1, '<center>Modification approval is not pending.</center>', @userId
  1807. RETURN
  1808. END
  1809. IF EXISTS (SELECT 'X' FROM applicationUsers WHERE userId = @userId AND approvedBy IS NULL)
  1810. BEGIN --New record
  1811. BEGIN TRANSACTION
  1812. SET @modType = 'Reject'
  1813. EXEC [dbo].proc_GetColumnToRow @logParamMain, @logIdentifier, @userId, @oldValue OUTPUT
  1814. INSERT INTO #msg(errorCode, msg, id)
  1815. EXEC proc_applicationLogs 'i', NULL, @modType, @tableAlias, @userId, @user, @oldValue, @newValue, @module
  1816. IF EXISTS (SELECT 'x' FROM #msg WHERE errorCode <> '0')
  1817. BEGIN
  1818. IF @@TRANCOUNT > 0
  1819. ROLLBACK TRANSACTION
  1820. EXEC proc_errorHandler 1, 'Failed to reject the transaction.', @userId
  1821. RETURN
  1822. END
  1823. DELETE FROM applicationUsers WHERE userId = @userId
  1824. IF @@TRANCOUNT > 0
  1825. COMMIT TRANSACTION
  1826. END
  1827. ELSE
  1828. BEGIN
  1829. BEGIN TRANSACTION
  1830. SET @modType = 'Reject'
  1831. EXEC [dbo].proc_GetColumnToRow @logParamMain, @logIdentifier, @userId, @oldValue OUTPUT
  1832. INSERT INTO #msg(errorCode, msg, id)
  1833. EXEC proc_applicationLogs 'i', NULL, @modType, @tableAlias, @userId, @user, @oldValue, @newValue, @module
  1834. IF EXISTS (SELECT 'x' FROM #msg WHERE errorCode <> '0')
  1835. BEGIN
  1836. IF @@TRANCOUNT > 0
  1837. ROLLBACK TRANSACTION
  1838. EXEC proc_errorHandler 1, 'Failed to reject the transaction.', @userId
  1839. RETURN
  1840. END
  1841. DELETE FROM applicationUsersMod WHERE userId = @userId
  1842. DELETE FROM passwordHistory WHERE userName = (SELECT userName FROM applicationUsers WITH(NOLOCK) WHERE userId = @userId)
  1843. IF @@TRANCOUNT > 0
  1844. COMMIT TRANSACTION
  1845. END
  1846. EXEC proc_errorHandler 0, 'Changes rejected successfully.', @userId
  1847. END
  1848. ELSE IF @flag = 'approve'
  1849. BEGIN
  1850. DECLARE
  1851. @baseChanges CHAR(1) = 'N'
  1852. ,@roleChanges CHAR(1) = 'N'
  1853. ,@functionChanges CHAR(1) = 'N'
  1854. IF EXISTS(SELECT 'X' FROM applicationUsers WITH(NOLOCK) WHERE userId = @userId AND approvedBy IS NULL)
  1855. OR
  1856. EXISTS(SELECT 'X' FROM applicationUsersMod WITH(NOLOCK) WHERE userId = @userId)
  1857. BEGIN
  1858. SET @baseChanges = 'Y'
  1859. END
  1860. IF EXISTS(SELECT 'X' FROM applicationUserRolesMod WITH(NOLOCK) WHERE userId = @userId)
  1861. BEGIN
  1862. SET @roleChanges = 'Y'
  1863. END
  1864. IF EXISTS(SELECT 'X' FROM applicationUserFunctionsMod WITH(NOLOCK) WHERE userId = @userId)
  1865. BEGIN
  1866. SET @functionChanges = 'Y'
  1867. END
  1868. IF @baseChanges <> 'Y' AND @roleChanges <> 'Y' AND @functionChanges <> 'Y'
  1869. BEGIN
  1870. EXEC proc_errorHandler 1, '<center>Modification approval is not pending.</center>', @userId
  1871. RETURN
  1872. END
  1873. BEGIN TRANSACTION
  1874. IF @baseChanges = 'Y'
  1875. BEGIN
  1876. IF EXISTS (SELECT 'X' FROM applicationUsers WHERE approvedBy IS NULL AND userId = @userId)
  1877. SET @modType = 'I'
  1878. ELSE
  1879. SELECT @modType = modType FROM applicationUsersMod WHERE userId = @userId
  1880. IF @modType = 'I'
  1881. BEGIN --New record
  1882. UPDATE applicationUsers SET
  1883. isActive = 'Y'
  1884. ,approvedBy = @user
  1885. ,approvedDate= GETDATE()
  1886. WHERE userId = @userId
  1887. EXEC [dbo].proc_GetColumnToRow @logParamMain, @logIdentifier, @userId, @newValue OUTPUT
  1888. END
  1889. ELSE IF @modType = 'U'
  1890. BEGIN
  1891. EXEC [dbo].proc_GetColumnToRow @logParamMain, @logIdentifier, @userId, @oldValue OUTPUT
  1892. UPDATE main SET
  1893. main.firstName = mode.firstName
  1894. ,main.middleName = mode.middleName
  1895. ,main.lastName = mode.lastName
  1896. ,main.salutation = mode.salutation
  1897. ,main.gender = mode.gender
  1898. ,main.telephoneNo = mode.telephoneNo
  1899. ,main.mobileNo = mode.mobileNo
  1900. ,main.state = mode.state
  1901. ,main.district = mode.district
  1902. ,main.zip = mode.zip
  1903. ,main.[address] = mode.[address]
  1904. ,main.city = mode.city
  1905. ,main.countryId = mode.countryId
  1906. ,main.email = mode.email
  1907. ,main.agentId = mode.agentId
  1908. ,main.sessionTimeOutPeriod = mode.sessionTimeOutPeriod
  1909. ,main.tranApproveLimit = mode.tranApproveLimit
  1910. ,main.agentCrLimitAmt = mode.agentCrLimitAmt
  1911. ,main.loginTime = mode.loginTime
  1912. ,main.logoutTime = mode.logoutTime
  1913. ,main.userAccessLevel = mode.userAccessLevel
  1914. ,main.perDayTranLimit = mode.perDayTranLimit
  1915. ,main.fromSendTrnTime = mode.fromSendTrnTime
  1916. ,main.toSendTrnTime = mode.toSendTrnTime
  1917. ,main.fromPayTrnTime = mode.fromPayTrnTime
  1918. ,main.toPayTrnTime = mode.toPayTrnTime
  1919. ,main.fromRptViewTime = mode.fromRptViewTime
  1920. ,main.toRptViewTime = mode.toRptViewTime
  1921. ,main.pwdChangeDays = mode.pwdChangeDays
  1922. ,main.pwdChangeWarningDays = mode.pwdChangeWarningDays
  1923. ,main.maxReportViewDays = mode.maxReportViewDays
  1924. ,main.modifiedDate = GETDATE()
  1925. ,main.modifiedBy = @user
  1926. ,main.userType = mode.userType
  1927. ,main.isActive = mode.isActive
  1928. FROM applicationUsers main
  1929. INNER JOIN applicationUsersMod mode ON mode.userId= main.userId
  1930. WHERE mode.userId = @userId
  1931. EXEC [dbo].proc_GetColumnToRow @logParamMain, @logIdentifier, @userId, @newValue OUTPUT
  1932. END
  1933. ELSE IF @modType = 'D'
  1934. BEGIN
  1935. EXEC [dbo].proc_GetColumnToRow @logParamMain, @logIdentifier, @userId, @oldValue OUTPUT
  1936. UPDATE applicationUsers SET
  1937. isDeleted = 'Y'
  1938. ,isActive = 'N'
  1939. ,modifiedDate = GETDATE()
  1940. ,modifiedBy = @user
  1941. WHERE userId = @userId
  1942. END
  1943. DELETE FROM applicationUsersMod WHERE userId = @userId
  1944. INSERT INTO #msg(errorCode, msg, id)
  1945. EXEC proc_applicationLogs 'i', NULL, @modType, @tableAlias, @userId, @user, @oldValue, @newValue, @module
  1946. END
  1947. IF @roleChanges = 'Y'
  1948. BEGIN
  1949. SELECT
  1950. @newValue = ISNULL(@newValue + ',', '') + CAST(roleId AS VARCHAR(50))
  1951. FROM applicationUserRolesMod
  1952. WHERE userId = @userId
  1953. EXEC [dbo].proc_GetColumnToRow 'applicationUserRoles', 'userId', @userId, @oldValue OUTPUT
  1954. DELETE FROM applicationUserRoles WHERE userId = @userId
  1955. INSERT applicationUserRoles(roleId, userId, createdBy, createdDate)
  1956. SELECT roleId, @userId, @user, GETDATE() FROM applicationUserRolesMod WHERE userId = @userId
  1957. DELETE FROM applicationUserRolesMod WHERE userId = @userId
  1958. INSERT INTO #msg(errorCode, msg, id)
  1959. EXEC proc_applicationLogs 'i', NULL, 'Update', 'User Roles', @userId, @user, @oldValue, @newValue, @module
  1960. END
  1961. IF @functionChanges = 'Y'
  1962. BEGIN
  1963. SELECT
  1964. @newValue = ISNULL(@newValue + ',', '') + CAST(functionId AS VARCHAR(50))
  1965. FROM applicationUserFunctionsMod
  1966. WHERE userId = @userId
  1967. EXEC [dbo].proc_GetColumnToRow 'applicationUserFunctions', 'userId', @userId, @oldValue OUTPUT
  1968. DELETE FROM applicationUserFunctions WHERE userId = @userId
  1969. INSERT applicationUserFunctions(functionId, userId, createdBy, createdDate)
  1970. SELECT functionId, @userId, @user, GETDATE() FROM applicationUserFunctionsMod WHERE userId = @userId
  1971. DELETE FROM applicationUserFunctionsMod WHERE userId = @userId
  1972. INSERT INTO #msg(errorCode, msg, id)
  1973. EXEC proc_applicationLogs 'i', NULL, 'Update', 'User Functions', @userId, @user, @oldValue, @newValue, @module
  1974. END
  1975. IF EXISTS (SELECT 'x' FROM #msg WHERE errorCode <> '0')
  1976. BEGIN
  1977. IF @@TRANCOUNT > 0
  1978. ROLLBACK TRANSACTION
  1979. EXEC proc_errorHandler 1, 'Could not approve the changes.', @userId
  1980. RETURN
  1981. END
  1982. IF @@TRANCOUNT > 0
  1983. COMMIT TRANSACTION
  1984. EXEC proc_errorHandler 0, 'Changes approved successfully.', @userId
  1985. END
  1986. END TRY
  1987. BEGIN CATCH
  1988. IF @@TRANCOUNT > 0
  1989. ROLLBACK TRANSACTION
  1990. SELECT 1 errorCode, ERROR_MESSAGE() mes, null id
  1991. END CATCH
  1992. GO