Leaked source code of windows server 2003
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.

2362 lines
57 KiB

  1. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[FK_UserAccessLevels_AccessLevels]') and OBJECTPROPERTY(id, N'IsForeignKey') = 1)
  2. ALTER TABLE [dbo].[UserAccessLevels] DROP CONSTRAINT FK_UserAccessLevels_AccessLevels
  3. GO
  4. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[FK_UserAccessLevels_AuthorizedUsers]') and OBJECTPROPERTY(id, N'IsForeignKey') = 1)
  5. ALTER TABLE [dbo].[UserAccessLevels] DROP CONSTRAINT FK_UserAccessLevels_AuthorizedUsers
  6. GO
  7. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[AddRestrictedCabAccess]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
  8. drop procedure [dbo].[AddRestrictedCabAccess]
  9. GO
  10. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[AddUserAndLevel]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
  11. drop procedure [dbo].[AddUserAndLevel]
  12. GO
  13. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[AddUserForApproval]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
  14. drop procedure [dbo].[AddUserForApproval]
  15. GO
  16. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[AddUserLevel]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
  17. drop procedure [dbo].[AddUserLevel]
  18. GO
  19. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[ApproveUser]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
  20. drop procedure [dbo].[ApproveUser]
  21. GO
  22. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[CheckApprovalAccess]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
  23. drop procedure [dbo].[CheckApprovalAccess]
  24. GO
  25. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[CheckRestrictedCabAccess]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
  26. drop procedure [dbo].[CheckRestrictedCabAccess]
  27. GO
  28. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[CheckUserAccess]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
  29. drop procedure [dbo].[CheckUserAccess]
  30. GO
  31. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[CheckUserAccessApprovals]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
  32. drop procedure [dbo].[CheckUserAccessApprovals]
  33. GO
  34. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[DefragIndexes]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
  35. drop procedure [dbo].[DefragIndexes]
  36. GO
  37. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetUserID]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
  38. drop procedure [dbo].[GetUserID]
  39. GO
  40. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[OcaAddUserAndLevel]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
  41. drop procedure [dbo].[OcaAddUserAndLevel]
  42. GO
  43. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[OcaAddUserForApproval]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
  44. drop procedure [dbo].[OcaAddUserForApproval]
  45. GO
  46. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[OcaAddUserLevel]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
  47. drop procedure [dbo].[OcaAddUserLevel]
  48. GO
  49. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[OcaApproveUser]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
  50. drop procedure [dbo].[OcaApproveUser]
  51. GO
  52. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[OcaCheckUserAccess]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
  53. drop procedure [dbo].[OcaCheckUserAccess]
  54. GO
  55. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[OcaCheckUserAccessApprovals]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
  56. drop procedure [dbo].[OcaCheckUserAccessApprovals]
  57. GO
  58. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[OcaGetUserID]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
  59. drop procedure [dbo].[OcaGetUserID]
  60. GO
  61. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[OcaRemoveUserLevel]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
  62. drop procedure [dbo].[OcaRemoveUserLevel]
  63. GO
  64. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[RemoveUserLevel]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
  65. drop procedure [dbo].[RemoveUserLevel]
  66. GO
  67. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[TestProc]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
  68. drop procedure [dbo].[TestProc]
  69. GO
  70. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[rolemember]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
  71. drop procedure [dbo].[rolemember]
  72. GO
  73. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[updatestats]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
  74. drop procedure [dbo].[updatestats]
  75. GO
  76. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Authorization_All]') and OBJECTPROPERTY(id, N'IsView') = 1)
  77. drop view [dbo].[Authorization_All]
  78. GO
  79. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[UserList]') and OBJECTPROPERTY(id, N'IsView') = 1)
  80. drop view [dbo].[UserList]
  81. GO
  82. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[ApprovalEmailList]') and OBJECTPROPERTY(id, N'IsView') = 1)
  83. drop view [dbo].[ApprovalEmailList]
  84. GO
  85. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[ApprovalJeff]') and OBJECTPROPERTY(id, N'IsView') = 1)
  86. drop view [dbo].[ApprovalJeff]
  87. GO
  88. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[ApprovalList]') and OBJECTPROPERTY(id, N'IsView') = 1)
  89. drop view [dbo].[ApprovalList]
  90. GO
  91. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[CabsEmailList]') and OBJECTPROPERTY(id, N'IsView') = 1)
  92. drop view [dbo].[CabsEmailList]
  93. GO
  94. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[CabsToBeCopied]') and OBJECTPROPERTY(id, N'IsView') = 1)
  95. drop view [dbo].[CabsToBeCopied]
  96. GO
  97. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[CabsToBeDeleted]') and OBJECTPROPERTY(id, N'IsView') = 1)
  98. drop view [dbo].[CabsToBeDeleted]
  99. GO
  100. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[CabsWaitingForApproval]') and OBJECTPROPERTY(id, N'IsView') = 1)
  101. drop view [dbo].[CabsWaitingForApproval]
  102. GO
  103. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[CopiedCabsList]') and OBJECTPROPERTY(id, N'IsView') = 1)
  104. drop view [dbo].[CopiedCabsList]
  105. GO
  106. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[OcaApprovalList]') and OBJECTPROPERTY(id, N'IsView') = 1)
  107. drop view [dbo].[OcaApprovalList]
  108. GO
  109. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[OcaUserList]') and OBJECTPROPERTY(id, N'IsView') = 1)
  110. drop view [dbo].[OcaUserList]
  111. GO
  112. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[RestrictedCabsList]') and OBJECTPROPERTY(id, N'IsView') = 1)
  113. drop view [dbo].[RestrictedCabsList]
  114. GO
  115. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[AccessLevels]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
  116. drop table [dbo].[AccessLevels]
  117. GO
  118. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[ApprovalTypes]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
  119. drop table [dbo].[ApprovalTypes]
  120. GO
  121. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Approvals]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
  122. drop table [dbo].[Approvals]
  123. GO
  124. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[AuthorizedUsers]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
  125. drop table [dbo].[AuthorizedUsers]
  126. GO
  127. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[CabAccess]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
  128. drop table [dbo].[CabAccess]
  129. GO
  130. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[CabAccessStatus]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
  131. drop table [dbo].[CabAccessStatus]
  132. GO
  133. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[OcaAccessLevels]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
  134. drop table [dbo].[OcaAccessLevels]
  135. GO
  136. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[OcaApprovalTypes]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
  137. drop table [dbo].[OcaApprovalTypes]
  138. GO
  139. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[OcaApprovals]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
  140. drop table [dbo].[OcaApprovals]
  141. GO
  142. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[OcaAuthorizedUsers]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
  143. drop table [dbo].[OcaAuthorizedUsers]
  144. GO
  145. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[OcaUserAccessLevels]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
  146. drop table [dbo].[OcaUserAccessLevels]
  147. GO
  148. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[UserAccessLevels]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
  149. drop table [dbo].[UserAccessLevels]
  150. GO
  151. CREATE TABLE [dbo].[AccessLevels] (
  152. [AccessLevelID] [int] NOT NULL ,
  153. [AccessDescription] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
  154. ) ON [PRIMARY]
  155. GO
  156. CREATE TABLE [dbo].[ApprovalTypes] (
  157. [ApprovalTypeID] [int] NOT NULL ,
  158. [ApproverAccessLevelID] [int] NOT NULL ,
  159. [ApprovalDescription] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
  160. ) ON [PRIMARY]
  161. GO
  162. CREATE TABLE [dbo].[Approvals] (
  163. [UserID] [int] NOT NULL ,
  164. [ApprovalTypeID] [int] NOT NULL ,
  165. [ApproverUserID] [int] NULL ,
  166. [Reason] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
  167. [DateApproved] [datetime] NULL ,
  168. [ApproverEmailStatus] [int] NULL ,
  169. [RequesterEmailStatus] [int] NULL
  170. ) ON [PRIMARY]
  171. GO
  172. CREATE TABLE [dbo].[AuthorizedUsers] (
  173. [UserID] [int] IDENTITY (1, 1) NOT NULL ,
  174. [UserAlias] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
  175. [UserDomain] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
  176. [DateSignedDCP] [datetime] NULL ,
  177. [WebSiteID] [int] NULL
  178. ) ON [PRIMARY]
  179. GO
  180. CREATE TABLE [dbo].[CabAccess] (
  181. [CabAccessID] [int] IDENTITY (1, 1) NOT NULL ,
  182. [iDatabase] [int] NOT NULL ,
  183. [iBucket] [int] NOT NULL ,
  184. [CabFilename] [varchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
  185. [CabPath] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
  186. [UserID] [int] NOT NULL ,
  187. [StatusID] [int] NOT NULL ,
  188. [DestCabPathFile] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
  189. [DateCopied] [datetime] NULL ,
  190. [DateRequested] [datetime] NULL
  191. ) ON [PRIMARY]
  192. GO
  193. CREATE TABLE [dbo].[CabAccessStatus] (
  194. [StatusID] [int] NOT NULL ,
  195. [StatusDescription] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
  196. ) ON [PRIMARY]
  197. GO
  198. CREATE TABLE [dbo].[OcaAccessLevels] (
  199. [AccessLevelID] [int] NOT NULL ,
  200. [AccessDescription] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
  201. ) ON [PRIMARY]
  202. GO
  203. CREATE TABLE [dbo].[OcaApprovalTypes] (
  204. [ApprovalTypeID] [int] NOT NULL ,
  205. [ApproverAccessLevelID] [int] NOT NULL ,
  206. [ApprovalDescription] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
  207. ) ON [PRIMARY]
  208. GO
  209. CREATE TABLE [dbo].[OcaApprovals] (
  210. [UserID] [int] NOT NULL ,
  211. [ApprovalTypeID] [int] NOT NULL ,
  212. [ApproverUserID] [int] NULL ,
  213. [Reason] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
  214. [DateApproved] [datetime] NULL
  215. ) ON [PRIMARY]
  216. GO
  217. CREATE TABLE [dbo].[OcaAuthorizedUsers] (
  218. [UserID] [int] IDENTITY (1, 1) NOT NULL ,
  219. [UserAlias] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
  220. [UserDomain] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
  221. [DateSignedDCP] [datetime] NULL
  222. ) ON [PRIMARY]
  223. GO
  224. CREATE TABLE [dbo].[OcaUserAccessLevels] (
  225. [UserAccessLevelID] [int] IDENTITY (1, 1) NOT NULL ,
  226. [UserID] [int] NOT NULL ,
  227. [AccessLevelID] [int] NOT NULL
  228. ) ON [PRIMARY]
  229. GO
  230. CREATE TABLE [dbo].[UserAccessLevels] (
  231. [UserAccessLevelID] [int] IDENTITY (1, 1) NOT NULL ,
  232. [UserID] [int] NOT NULL ,
  233. [AccessLevelID] [int] NOT NULL
  234. ) ON [PRIMARY]
  235. GO
  236. ALTER TABLE [dbo].[CabAccess] WITH NOCHECK ADD
  237. CONSTRAINT [PK_CabAccess] PRIMARY KEY CLUSTERED
  238. (
  239. [CabAccessID]
  240. ) ON [PRIMARY]
  241. GO
  242. ALTER TABLE [dbo].[CabAccessStatus] WITH NOCHECK ADD
  243. CONSTRAINT [PK_CabAccessStatus] PRIMARY KEY CLUSTERED
  244. (
  245. [StatusID]
  246. ) ON [PRIMARY]
  247. GO
  248. ALTER TABLE [dbo].[OcaAccessLevels] WITH NOCHECK ADD
  249. CONSTRAINT [PK_OcaAccessLevels] PRIMARY KEY CLUSTERED
  250. (
  251. [AccessLevelID]
  252. ) ON [PRIMARY]
  253. GO
  254. ALTER TABLE [dbo].[OcaApprovalTypes] WITH NOCHECK ADD
  255. CONSTRAINT [PK_OcaApprovalTypes] PRIMARY KEY CLUSTERED
  256. (
  257. [ApprovalTypeID]
  258. ) ON [PRIMARY]
  259. GO
  260. ALTER TABLE [dbo].[OcaApprovals] WITH NOCHECK ADD
  261. CONSTRAINT [PK_OcaApprovals] PRIMARY KEY CLUSTERED
  262. (
  263. [UserID],
  264. [ApprovalTypeID]
  265. ) ON [PRIMARY]
  266. GO
  267. ALTER TABLE [dbo].[OcaAuthorizedUsers] WITH NOCHECK ADD
  268. CONSTRAINT [PK_OcaAuthorizedUsers] PRIMARY KEY CLUSTERED
  269. (
  270. [UserID]
  271. ) ON [PRIMARY]
  272. GO
  273. ALTER TABLE [dbo].[OcaUserAccessLevels] WITH NOCHECK ADD
  274. CONSTRAINT [PK_OcaUserAccessLevels] PRIMARY KEY CLUSTERED
  275. (
  276. [UserAccessLevelID]
  277. ) ON [PRIMARY]
  278. GO
  279. ALTER TABLE [dbo].[UserAccessLevels] WITH NOCHECK ADD
  280. CONSTRAINT [PK_UserAccessLevels] PRIMARY KEY CLUSTERED
  281. (
  282. [UserAccessLevelID]
  283. ) ON [PRIMARY]
  284. GO
  285. ALTER TABLE [dbo].[AccessLevels] WITH NOCHECK ADD
  286. CONSTRAINT [PK_AccessLevels] PRIMARY KEY NONCLUSTERED
  287. (
  288. [AccessLevelID]
  289. ) ON [PRIMARY]
  290. GO
  291. ALTER TABLE [dbo].[ApprovalTypes] WITH NOCHECK ADD
  292. CONSTRAINT [PK_ApprovalTypes] PRIMARY KEY NONCLUSTERED
  293. (
  294. [ApprovalTypeID]
  295. ) ON [PRIMARY]
  296. GO
  297. ALTER TABLE [dbo].[Approvals] WITH NOCHECK ADD
  298. CONSTRAINT [PK_Approvals] PRIMARY KEY NONCLUSTERED
  299. (
  300. [UserID],
  301. [ApprovalTypeID]
  302. ) ON [PRIMARY]
  303. GO
  304. CREATE INDEX [UserID] ON [dbo].[Approvals]([UserID]) ON [PRIMARY]
  305. GO
  306. ALTER TABLE [dbo].[AuthorizedUsers] WITH NOCHECK ADD
  307. CONSTRAINT [PK_AuthorizedUsers] PRIMARY KEY NONCLUSTERED
  308. (
  309. [UserID]
  310. ) ON [PRIMARY]
  311. GO
  312. CREATE UNIQUE INDEX [UserAndDomain] ON [dbo].[AuthorizedUsers]([UserAlias], [UserDomain]) ON [PRIMARY]
  313. GO
  314. CREATE INDEX [UserID] ON [dbo].[UserAccessLevels]([UserID]) ON [PRIMARY]
  315. GO
  316. ALTER TABLE [dbo].[UserAccessLevels] ADD
  317. CONSTRAINT [FK_UserAccessLevels_AccessLevels] FOREIGN KEY
  318. (
  319. [AccessLevelID]
  320. ) REFERENCES [dbo].[AccessLevels] (
  321. [AccessLevelID]
  322. ),
  323. CONSTRAINT [FK_UserAccessLevels_AuthorizedUsers] FOREIGN KEY
  324. (
  325. [UserID]
  326. ) REFERENCES [dbo].[AuthorizedUsers] (
  327. [UserID]
  328. )
  329. GO
  330. SET QUOTED_IDENTIFIER ON
  331. GO
  332. SET ANSI_NULLS ON
  333. GO
  334. CREATE VIEW dbo.ApprovalEmailList
  335. AS
  336. SELECT dbo.Approvals.Reason, dbo.AuthorizedUsers.UserAlias, dbo.AuthorizedUsers.UserDomain, dbo.Approvals.ApprovalTypeID,
  337. dbo.AuthorizedUsers.UserID, dbo.ApprovalTypes.ApproverAccessLevelID, dbo.Approvals.ApproverEmailStatus, dbo.Approvals.RequesterEmailStatus,
  338. AuthorizedUsers_1.UserAlias AS ApproverUserAlias
  339. FROM dbo.Approvals INNER JOIN
  340. dbo.AuthorizedUsers ON dbo.Approvals.UserID = dbo.AuthorizedUsers.UserID INNER JOIN
  341. dbo.ApprovalTypes ON dbo.Approvals.ApprovalTypeID = dbo.ApprovalTypes.ApprovalTypeID LEFT OUTER JOIN
  342. dbo.AuthorizedUsers AuthorizedUsers_1 ON dbo.Approvals.ApproverUserID = AuthorizedUsers_1.UserID
  343. WHERE (dbo.Approvals.ApproverEmailStatus = 0) AND (dbo.Approvals.ApprovalTypeID = 1) OR
  344. (dbo.Approvals.RequesterEmailStatus = 0) AND (dbo.Approvals.ApprovalTypeID = 1)
  345. GO
  346. SET QUOTED_IDENTIFIER OFF
  347. GO
  348. SET ANSI_NULLS ON
  349. GO
  350. SET QUOTED_IDENTIFIER ON
  351. GO
  352. SET ANSI_NULLS ON
  353. GO
  354. CREATE VIEW dbo.ApprovalJeff
  355. AS
  356. SELECT TOP 100 PERCENT
  357. Approvals.UserId,
  358. UserAlias,
  359. UserDomain,
  360. Reason,
  361. Dateapproved
  362. FROM
  363. Approvals
  364. INNER JOIN
  365. AuthorizedUsers
  366. ON
  367. AuthorizedUsers.UserId = Approvals.UserId
  368. ORDER BY
  369. Approvals.UserId
  370. GO
  371. SET QUOTED_IDENTIFIER OFF
  372. GO
  373. SET ANSI_NULLS ON
  374. GO
  375. SET QUOTED_IDENTIFIER ON
  376. GO
  377. SET ANSI_NULLS ON
  378. GO
  379. CREATE VIEW dbo.ApprovalList
  380. AS
  381. SELECT Approvals.UserID, Approvals.ApprovalTypeID,
  382. Approvals.Reason, Approvals.DateApproved,
  383. ApprovalTypes.ApprovalDescription,
  384. AuthorizedUsers.UserAlias AS ApproverAlias,
  385. AuthorizedUsers.UserDomain AS ApproverDomain,
  386. Approvals.ApproverUserID,
  387. ApprovalTypes.ApproverAccessLevelID AS ApproverAccessLevelID
  388. FROM Approvals INNER JOIN
  389. ApprovalTypes ON
  390. Approvals.ApprovalTypeID = ApprovalTypes.ApprovalTypeID LEFT
  391. OUTER JOIN
  392. AuthorizedUsers ON
  393. Approvals.ApproverUserID = AuthorizedUsers.UserID
  394. GO
  395. SET QUOTED_IDENTIFIER OFF
  396. GO
  397. SET ANSI_NULLS ON
  398. GO
  399. SET QUOTED_IDENTIFIER ON
  400. GO
  401. SET ANSI_NULLS ON
  402. GO
  403. CREATE VIEW dbo.CabsEmailList
  404. AS
  405. SELECT dbo.Approvals.Reason, dbo.AuthorizedUsers.UserAlias, dbo.AuthorizedUsers.UserDomain, dbo.Approvals.ApprovalTypeID,
  406. dbo.AuthorizedUsers.UserID, dbo.ApprovalTypes.ApproverAccessLevelID, dbo.Approvals.ApproverEmailStatus, dbo.Approvals.RequesterEmailStatus,
  407. AuthorizedUsers_1.UserAlias AS ApproverUserAlias
  408. FROM dbo.Approvals INNER JOIN
  409. dbo.AuthorizedUsers ON dbo.Approvals.UserID = dbo.AuthorizedUsers.UserID INNER JOIN
  410. dbo.ApprovalTypes ON dbo.Approvals.ApprovalTypeID = dbo.ApprovalTypes.ApprovalTypeID LEFT OUTER JOIN
  411. dbo.AuthorizedUsers AuthorizedUsers_1 ON dbo.Approvals.ApproverUserID = AuthorizedUsers_1.UserID
  412. WHERE (dbo.Approvals.ApproverEmailStatus = 0) AND (dbo.Approvals.ApprovalTypeID = 2)
  413. GO
  414. SET QUOTED_IDENTIFIER OFF
  415. GO
  416. SET ANSI_NULLS ON
  417. GO
  418. SET QUOTED_IDENTIFIER OFF
  419. GO
  420. SET ANSI_NULLS ON
  421. GO
  422. CREATE VIEW dbo.CabsToBeCopied
  423. AS
  424. SELECT CabAccess.CabAccessID, CabAccess.CabFilename, CabAccess.CabPath, CabAccess.UserID, AuthorizedUsers.UserAlias,
  425. AuthorizedUsers.UserDomain, CabAccess.iDatabase, CabAccess.iBucket
  426. FROM dbo.CabAccess CabAccess INNER JOIN
  427. dbo.AuthorizedUsers AuthorizedUsers ON CabAccess.UserID = AuthorizedUsers.UserID INNER JOIN
  428. dbo.Approvals ON CabAccess.UserID = dbo.Approvals.UserID
  429. WHERE (CabAccess.StatusID = 1) AND (dbo.Approvals.ApproverUserID IS NOT NULL) AND (dbo.Approvals.DateApproved IS NOT NULL) AND
  430. (dbo.Approvals.Reason IS NOT NULL) AND (dbo.Approvals.UserID IS NOT NULL) AND (dbo.Approvals.ApprovalTypeID = 2)
  431. GO
  432. SET QUOTED_IDENTIFIER OFF
  433. GO
  434. SET ANSI_NULLS ON
  435. GO
  436. SET QUOTED_IDENTIFIER ON
  437. GO
  438. SET ANSI_NULLS ON
  439. GO
  440. CREATE VIEW dbo.CabsToBeDeleted
  441. AS
  442. SELECT CabAccess.CabAccessID, CabAccess.CabFilename, CabAccess.UserID, AuthorizedUsers.UserAlias
  443. FROM dbo.CabAccess CabAccess INNER JOIN
  444. dbo.AuthorizedUsers AuthorizedUsers ON CabAccess.UserID = AuthorizedUsers.UserID
  445. WHERE (CabAccess.StatusID = 2) AND (CURRENT_TIMESTAMP > DATEADD(day, 14, CabAccess.DateCopied))
  446. GO
  447. SET QUOTED_IDENTIFIER OFF
  448. GO
  449. SET ANSI_NULLS ON
  450. GO
  451. SET QUOTED_IDENTIFIER ON
  452. GO
  453. SET ANSI_NULLS ON
  454. GO
  455. CREATE VIEW dbo.CabsWaitingForApproval
  456. AS
  457. SELECT CabFilename, CabPath, UserID, iDatabase, iBucket
  458. FROM dbo.CabAccess
  459. WHERE (StatusID = 1)
  460. GO
  461. SET QUOTED_IDENTIFIER OFF
  462. GO
  463. SET ANSI_NULLS ON
  464. GO
  465. SET QUOTED_IDENTIFIER ON
  466. GO
  467. SET ANSI_NULLS ON
  468. GO
  469. CREATE VIEW dbo.CopiedCabsList
  470. AS
  471. SELECT dbo.CabAccess.DestCabPathFile, dbo.CabAccess.DateCopied, dbo.AuthorizedUsers.UserAlias, dbo.AuthorizedUsers.UserDomain,
  472. dbo.CabAccess.CabFilename, dbo.CabAccess.iBucket, dbo.CabAccess.iDatabase, dbo.CabAccess.CabPath
  473. FROM dbo.CabAccess INNER JOIN
  474. dbo.AuthorizedUsers ON dbo.CabAccess.UserID = dbo.AuthorizedUsers.UserID AND dbo.CabAccess.StatusID = 2
  475. GO
  476. SET QUOTED_IDENTIFIER OFF
  477. GO
  478. SET ANSI_NULLS ON
  479. GO
  480. SET QUOTED_IDENTIFIER ON
  481. GO
  482. SET ANSI_NULLS ON
  483. GO
  484. CREATE VIEW dbo.OcaApprovalList
  485. AS
  486. SELECT OcaApprovals.UserID, OcaApprovals.ApprovalTypeID,
  487. OcaApprovals.Reason, OcaApprovals.DateApproved,
  488. OcaApprovalTypes.ApprovalDescription,
  489. OcaAuthorizedUsers.UserAlias AS ApproverAlias,
  490. OcaAuthorizedUsers.UserDomain AS ApproverDomain,
  491. OcaApprovals.ApproverUserID,
  492. OcaApprovalTypes.ApproverAccessLevelID AS ApproverAccessLevelID
  493. FROM OcaApprovals INNER JOIN
  494. OcaApprovalTypes ON
  495. OcaApprovals.ApprovalTypeID = OcaApprovalTypes.ApprovalTypeID LEFT
  496. OUTER JOIN
  497. OcaAuthorizedUsers ON
  498. OcaApprovals.ApproverUserID = OcaAuthorizedUsers.UserID
  499. GO
  500. SET QUOTED_IDENTIFIER OFF
  501. GO
  502. SET ANSI_NULLS ON
  503. GO
  504. SET QUOTED_IDENTIFIER ON
  505. GO
  506. SET ANSI_NULLS ON
  507. GO
  508. CREATE VIEW dbo.OcaUserList
  509. AS
  510. SELECT OcaAuthorizedUsers.UserID, OcaAuthorizedUsers.UserAlias,
  511. OcaAuthorizedUsers.UserDomain,
  512. OcaAuthorizedUsers.DateSignedDCP,
  513. OcaAccessLevels.AccessDescription,
  514. OcaAccessLevels.AccessLevelID,
  515. CASE WHEN COUNT(OcaApprovals.ApprovalTypeID)
  516. > 0 THEN 'Yes' ELSE 'No' END 'HasApprovals',
  517. case when count(Ocaapprovals.ApproverUserID)<>count(Ocaapprovals.approvaltypeid) then 'Yes' ELSE 'No' END 'NeedsApproval'
  518. FROM OcaAuthorizedUsers INNER JOIN
  519. OcaUserAccessLevels ON
  520. OcaAuthorizedUsers.UserID = OcaUserAccessLevels.UserID INNER JOIN
  521. OcaAccessLevels ON
  522. OcaUserAccessLevels.AccessLevelID = OcaAccessLevels.AccessLevelID
  523. LEFT OUTER JOIN
  524. OcaApprovals ON
  525. OcaAuthorizedUsers.UserID = OcaApprovals.UserID
  526. GROUP BY OcaAuthorizedUsers.UserID, OcaAuthorizedUsers.UserAlias,
  527. OcaAuthorizedUsers.UserDomain,
  528. OcaAuthorizedUsers.DateSignedDCP,
  529. OcaAccessLevels.AccessDescription,
  530. OcaAccessLevels.AccessLevelID
  531. GO
  532. SET QUOTED_IDENTIFIER OFF
  533. GO
  534. SET ANSI_NULLS ON
  535. GO
  536. SET QUOTED_IDENTIFIER ON
  537. GO
  538. SET ANSI_NULLS ON
  539. GO
  540. CREATE VIEW dbo.RestrictedCabsList
  541. AS
  542. SELECT TOP 100 PERCENT dbo.AuthorizedUsers.UserAlias, dbo.AuthorizedUsers.UserDomain, dbo.CabAccessStatus.StatusDescription,
  543. dbo.CabAccess.DateCopied, dbo.Approvals.ApproverEmailStatus, dbo.Approvals.RequesterEmailStatus, dbo.Approvals.DateApproved,
  544. dbo.Approvals.ApproverUserID, dbo.CabAccess.iBucket, dbo.CabAccess.iDatabase, dbo.CabAccess.DestCabPathFile,
  545. dbo.CabAccess.DateRequested
  546. FROM dbo.CabAccess INNER JOIN
  547. dbo.CabAccessStatus ON dbo.CabAccess.StatusID = dbo.CabAccessStatus.StatusID INNER JOIN
  548. dbo.AuthorizedUsers ON dbo.CabAccess.UserID = dbo.AuthorizedUsers.UserID LEFT OUTER JOIN
  549. dbo.Approvals ON dbo.CabAccess.UserID = dbo.Approvals.UserID
  550. WHERE (dbo.Approvals.ApprovalTypeID = 2)
  551. ORDER BY dbo.CabAccess.DateRequested DESC, dbo.CabAccess.DateCopied DESC
  552. GO
  553. SET QUOTED_IDENTIFIER OFF
  554. GO
  555. SET ANSI_NULLS ON
  556. GO
  557. SET QUOTED_IDENTIFIER OFF
  558. GO
  559. SET ANSI_NULLS ON
  560. GO
  561. CREATE VIEW dbo.Authorization_All
  562. AS
  563. SELECT TOP 100 PERCENT dbo.AccessLevels.AccessDescription, dbo.AuthorizedUsers.UserAlias, dbo.AuthorizedUsers.UserDomain,
  564. dbo.AuthorizedUsers.DateSignedDCP
  565. FROM dbo.AccessLevels INNER JOIN
  566. dbo.UserAccessLevels ON dbo.AccessLevels.AccessLevelID = dbo.UserAccessLevels.AccessLevelID INNER JOIN
  567. dbo.AuthorizedUsers ON dbo.UserAccessLevels.UserID = dbo.AuthorizedUsers.UserID
  568. ORDER BY dbo.AuthorizedUsers.UserAlias
  569. GO
  570. SET QUOTED_IDENTIFIER OFF
  571. GO
  572. SET ANSI_NULLS ON
  573. GO
  574. SET QUOTED_IDENTIFIER OFF
  575. GO
  576. SET ANSI_NULLS ON
  577. GO
  578. CREATE VIEW dbo.UserList
  579. AS
  580. SELECT AuthorizedUsers.UserID, AuthorizedUsers.UserAlias,
  581. AuthorizedUsers.UserDomain,
  582. AuthorizedUsers.DateSignedDCP,
  583. AccessLevels.AccessDescription,
  584. AccessLevels.AccessLevelID,
  585. CASE WHEN COUNT(Approvals.ApprovalTypeID)
  586. > 0 THEN 'Yes' ELSE 'No' END 'HasApprovals',
  587. case when count(approvals.ApproverUserID)<>count(approvals.approvaltypeid) then 'Yes' ELSE 'No' END 'NeedsApproval'
  588. FROM AuthorizedUsers INNER JOIN
  589. UserAccessLevels ON
  590. AuthorizedUsers.UserID = UserAccessLevels.UserID INNER JOIN
  591. AccessLevels ON
  592. UserAccessLevels.AccessLevelID = AccessLevels.AccessLevelID
  593. LEFT OUTER JOIN
  594. Approvals ON
  595. AuthorizedUsers.UserID = Approvals.UserID
  596. GROUP BY AuthorizedUsers.UserID, AuthorizedUsers.UserAlias,
  597. AuthorizedUsers.UserDomain,
  598. AuthorizedUsers.DateSignedDCP,
  599. AccessLevels.AccessDescription,
  600. AccessLevels.AccessLevelID
  601. GO
  602. SET QUOTED_IDENTIFIER OFF
  603. GO
  604. SET ANSI_NULLS ON
  605. GO
  606. SET QUOTED_IDENTIFIER ON
  607. GO
  608. SET ANSI_NULLS OFF
  609. GO
  610. CREATE PROCEDURE AddRestrictedCabAccess
  611. @User varchar(50),
  612. @Domain varchar(50),
  613. @iDatabase int,
  614. @iBucket int,
  615. @szCabFilename varchar(100),
  616. @szCabPath varchar(255)
  617. As
  618. set nocount on
  619. -- local var to hold record id
  620. declare @target int
  621. declare @userfound int
  622. declare @userApproved int
  623. declare @CabAccessID int
  624. SELECT @userfound = UserID FROM AuthorizedUsers WHERE UserAlias = @User and UserDomain = @Domain
  625. if @userfound is NULL
  626. begin
  627. select @target = -1
  628. return @target
  629. end
  630. SELECT @userApproved = UserID FROM Approvals WHERE UserID = @userfound
  631. if @userApproved is NULL
  632. begin
  633. select @target = -1
  634. return @target
  635. end
  636. SELECT @CabAccessID= CabAccessID from CabAccess WHERE UserID = @userfound and iBucket=@iBucket and iDatabase=@iDatabase
  637. and CabFilename=@szCabFilename and CabPath=@szCabPath
  638. if @CabAccessID is NULL
  639. begin
  640. INSERT INTO CabAccess (UserID,iDatabase, iBucket, CabFilename, CabPath, StatusID, DateRequested)
  641. VALUES (@userfound,@iDatabase, @iBucket, @szCabFilename, @szCabPath, 1, CURRENT_TIMESTAMP)
  642. end
  643. else
  644. begin
  645. UPDATE CabAccess SET StatusID=1, DateRequested=CURRENT_TIMESTAMP WHERE (CabAccessID=@CabAccessID)
  646. end
  647. SELECT @CabAccessID= CabAccessID from CabAccess WHERE UserID = @userfound and iBucket=@iBucket and iDatabase=@iDatabase
  648. and CabFilename=@szCabFilename and CabPath = @szCabPath
  649. if @CabAccessID is NULL
  650. begin
  651. select @target = -1
  652. return @target
  653. end
  654. select @target = @CabAccessID
  655. RETURN @target
  656. GO
  657. SET QUOTED_IDENTIFIER OFF
  658. GO
  659. SET ANSI_NULLS ON
  660. GO
  661. SET QUOTED_IDENTIFIER OFF
  662. GO
  663. SET ANSI_NULLS ON
  664. GO
  665. CREATE PROCEDURE AddUserAndLevel
  666. @User varchar(50),
  667. @Domain varchar(50),
  668. @CurrTime varchar(50),
  669. @Level int
  670. As
  671. set nocount on
  672. -- local var to hold record id
  673. declare @target int
  674. declare @levelDB int
  675. declare @userDB int
  676. declare @userID int
  677. -- do we already have this FileInCab?
  678. SELECT @userID = UserID FROM AuthorizedUsers WHERE
  679. UserAlias =@User AND UserDomain = @Domain
  680. if @userID is NULL
  681. begin -- not there, create new record
  682. INSERT INTO AuthorizedUsers (UserAlias, UserDomain, DateSignedDCP)
  683. VALUES (@User, @Domain,@CurrTime)
  684. SELECT @userID = UserID FROM AuthorizedUsers WHERE
  685. UserAlias =@User AND UserDomain = @Domain
  686. end
  687. else
  688. begin -- update record with new signing date/time
  689. UPDATE AuthorizedUsers SET DateSignedDCP = @CurrTime
  690. WHERE (UserID = @userID)
  691. end
  692. -- PRINT @userID
  693. -- we don't have the userid
  694. if @userID is NULL
  695. begin
  696. select @userID = 0
  697. return @userID
  698. end
  699. select @levelDB = AccessLevelID FROM AccessLevels WHERE
  700. AccessLevelID = @Level
  701. -- bad level passed in
  702. if @levelDB is NULL
  703. begin
  704. select @levelDB = 0
  705. return @levelDB
  706. end
  707. SELECT @target = UserAccessLevelID FROM UserAccessLevels WHERE
  708. UserID = @userID and AccessLevelID = @Level
  709. if @target is NULL
  710. begin -- not there, create new record
  711. INSERT INTO UserAccessLevels (UserID, AccessLevelID)
  712. VALUES (@userID, @Level)
  713. SELECT @target = UserAccessLevelID FROM UserAccessLevels WHERE
  714. UserID = @userID and AccessLevelID = @Level
  715. end
  716. if @target is NULL
  717. begin
  718. select @target = 0
  719. return @target
  720. end
  721. -- return the UserAccessLevelID
  722. RETURN @target
  723. GO
  724. SET QUOTED_IDENTIFIER OFF
  725. GO
  726. SET ANSI_NULLS ON
  727. GO
  728. SET QUOTED_IDENTIFIER ON
  729. GO
  730. SET ANSI_NULLS ON
  731. GO
  732. CREATE PROCEDURE AddUserForApproval
  733. @ApprovalType int,
  734. @User varchar(50),
  735. @Domain varchar(50),
  736. @Reason varchar(250)
  737. As
  738. set nocount on
  739. -- local var to hold record id
  740. declare @target int
  741. declare @userfound int
  742. declare @userApproved int
  743. SELECT @userfound = UserID FROM AuthorizedUsers WHERE UserAlias = @User and UserDomain = @Domain
  744. if @userfound is NULL
  745. begin
  746. select @target = -1
  747. return @target
  748. end
  749. SELECT @userApproved = UserID FROM Approvals WHERE UserID = @userfound and ApprovalTypeID=@ApprovalType
  750. if @userApproved is NULL
  751. begin -- not there, create new record
  752. INSERT INTO Approvals (UserID,ApprovalTypeID, Reason, ApproverEmailStatus)
  753. VALUES (@userfound,@ApprovalType,@Reason,0)
  754. SELECT @userApproved = UserID from Approvals WHERE UserID = @userfound
  755. if @userApproved is NULL
  756. begin
  757. select @target = -1
  758. return @target
  759. end
  760. end
  761. select @target = @userApproved
  762. RETURN @target
  763. GO
  764. SET QUOTED_IDENTIFIER OFF
  765. GO
  766. SET ANSI_NULLS ON
  767. GO
  768. SET QUOTED_IDENTIFIER OFF
  769. GO
  770. SET ANSI_NULLS ON
  771. GO
  772. CREATE PROCEDURE AddUserLevel
  773. @UserID int,
  774. @Level int
  775. As
  776. set nocount on
  777. -- local var to hold record id
  778. declare @target int
  779. declare @levelDB int
  780. declare @userDB int
  781. select @userDB = UserID from AuthorizedUsers WHERE
  782. UserID = @UserID
  783. if @userDB is NULL
  784. begin
  785. select @userDB = 0
  786. return @userDB
  787. end
  788. select @levelDB = AccessLevelID FROM AccessLevels WHERE
  789. AccessLevelID = @Level
  790. if @levelDB is NULL
  791. begin
  792. select @levelDB = 0
  793. return @levelDB
  794. end
  795. SELECT @target = UserAccessLevelID FROM UserAccessLevels WHERE
  796. UserID = @UserID and AccessLevelID = @Level
  797. if @target is NULL
  798. begin -- not there, create new record
  799. INSERT INTO UserAccessLevels (UserID, AccessLevelID)
  800. VALUES (@UserID, @Level)
  801. SELECT @target = UserAccessLevelID FROM UserAccessLevels WHERE
  802. UserID = @UserID and AccessLevelID = @Level
  803. end
  804. RETURN @target
  805. GO
  806. SET QUOTED_IDENTIFIER OFF
  807. GO
  808. SET ANSI_NULLS ON
  809. GO
  810. SET QUOTED_IDENTIFIER ON
  811. GO
  812. SET ANSI_NULLS ON
  813. GO
  814. CREATE PROCEDURE ApproveUser
  815. @UserID int,
  816. @ApprovalTypeID int,
  817. @ApproverUserID int,
  818. @DateApproved varchar(50)
  819. As
  820. set nocount on
  821. -- local var to hold record id
  822. declare @target int
  823. declare @userfound int
  824. declare @userApproved int
  825. SELECT @userApproved = UserID FROM Approvals WHERE UserID = @UserID and ApprovalTypeID = @ApprovalTypeID
  826. if @userApproved is not NULL
  827. begin -- not there, create new record
  828. UPDATE Approvals SET ApproverUserID = @ApproverUserID,
  829. DateApproved = @DateApproved,RequesterEmailStatus=0
  830. WHERE (UserID = @UserID and ApprovalTypeID = @ApprovalTypeID)
  831. end
  832. select @target = 0
  833. RETURN @target
  834. GO
  835. SET QUOTED_IDENTIFIER OFF
  836. GO
  837. SET ANSI_NULLS ON
  838. GO
  839. SET QUOTED_IDENTIFIER OFF
  840. GO
  841. SET ANSI_NULLS OFF
  842. GO
  843. -- return values:
  844. -- 0 = user not in database
  845. -- 1 = user in database or approved in approvals table
  846. -- 2 = user access is denied
  847. -- 3 = user in table, but not yet approved
  848. -- 4 = user not in approvals table
  849. CREATE PROCEDURE CheckApprovalAccess
  850. @User varchar(50),
  851. @Domain varchar(50),
  852. @Level int,
  853. @ApprovalTypeID int
  854. As
  855. set nocount on
  856. -- local var to hold record id
  857. declare @target int
  858. declare @HasApproval int
  859. declare @DBUserID int
  860. declare @ApproverUserID int
  861. declare @Reason varchar(255)
  862. declare @DateApproved varchar(50)
  863. declare @ApprovalsUserID int
  864. SELECT @DBUserID=AuthorizedUsers.UserID, @target=UserAccessLevels.AccessLevelID,
  865. @HasApproval= Approvals.UserID
  866. FROM UserAccessLevels INNER JOIN
  867. AuthorizedUsers ON
  868. UserAccessLevels.UserID = AuthorizedUsers.UserID LEFT OUTER
  869. JOIN
  870. Approvals ON
  871. AuthorizedUsers.UserID = Approvals.UserID
  872. WHERE (AuthorizedUsers.UserAlias = @User) AND
  873. (AuthorizedUsers.UserDomain = @Domain) AND
  874. ((UserAccessLevels.AccessLevelID = 0) OR
  875. ((UserAccessLevels.AccessLevelID = @Level) AND
  876. (AuthorizedUsers.DateSignedDCP IS NOT NULL) AND
  877. (CURRENT_TIMESTAMP <= DATEADD(month, 12,
  878. AuthorizedUsers.DateSignedDCP))))
  879. ORDER BY UserAccessLevels.AccessLevelID DESC
  880. -- @target will point to the last value we read
  881. if @target IS NULL
  882. begin
  883. select @target = 0
  884. return @target
  885. end
  886. else
  887. begin
  888. -- @target is 0 if access is denied, we return 2 in this case
  889. if @target = 0
  890. begin
  891. select @target = 2
  892. return @target
  893. end
  894. end
  895. SELECT @ApprovalsUserID = UserID,@ApproverUserID = ApproverUserID, @Reason = Reason, @DateApproved = DateApproved
  896. FROM Approvals
  897. WHERE UserID = @DBUserID AND ApprovalTypeID = @ApprovalTypeID
  898. -- not in this table, they need to be added
  899. if @ApprovalsUserID is NULL
  900. begin
  901. -- print 'okay to pass'
  902. select @target = 4
  903. return @target
  904. end
  905. if @ApproverUserID is NULL or @Reason is NULL or @DateApproved is NULL
  906. begin
  907. -- print 'not approved yet'
  908. select @target = 3
  909. return @target
  910. end
  911. -- print 'approved'
  912. select @target = 1
  913. RETURN @target
  914. GO
  915. SET QUOTED_IDENTIFIER OFF
  916. GO
  917. SET ANSI_NULLS ON
  918. GO
  919. SET QUOTED_IDENTIFIER OFF
  920. GO
  921. SET ANSI_NULLS OFF
  922. GO
  923. -- -1 = no access to this cab
  924. -- otherwise, statusid (which is >= 0)
  925. CREATE PROCEDURE CheckRestrictedCabAccess
  926. @User varchar(50),
  927. @Domain varchar(50),
  928. @iDatabase int,
  929. @iBucket int,
  930. @szCabFilename varchar(100),
  931. @szCabPath varchar(255)
  932. As
  933. set nocount on
  934. -- local var to hold record id
  935. declare @target int
  936. declare @userfound int
  937. declare @userApproved int
  938. declare @CabAccessID int
  939. declare @StatusID int
  940. SELECT @userfound = UserID FROM AuthorizedUsers WHERE UserAlias = @User and UserDomain = @Domain
  941. if @userfound is NULL
  942. begin
  943. select @target = -1
  944. return @target
  945. end
  946. SELECT @userApproved = UserID FROM Approvals WHERE UserID = @userfound
  947. if @userApproved is NULL
  948. begin
  949. select @target = -1
  950. return @target
  951. end
  952. SELECT @CabAccessID= CabAccessID, @StatusID=StatusID from CabAccess WHERE UserID = @userfound and iBucket=@iBucket and iDatabase=@iDatabase
  953. and CabFilename=@szCabFilename and CabPath =@szCabPath
  954. if @CabAccessID is NULL
  955. begin
  956. select @target = -1
  957. return @target
  958. end
  959. if @StatusID is NULL
  960. begin
  961. select @target = -1
  962. return @target
  963. end
  964. select @target = @StatusID
  965. RETURN @target
  966. GO
  967. SET QUOTED_IDENTIFIER OFF
  968. GO
  969. SET ANSI_NULLS ON
  970. GO
  971. SET QUOTED_IDENTIFIER OFF
  972. GO
  973. SET ANSI_NULLS ON
  974. GO
  975. CREATE PROCEDURE CheckUserAccess
  976. @User varchar(50),
  977. @Domain varchar(50),
  978. @Level int
  979. As
  980. set nocount on
  981. -- local var to hold record id
  982. declare @target int
  983. SELECT @target= dbo.UserAccessLevels.AccessLevelID FROM
  984. dbo.UserAccessLevels INNER JOIN dbo.AuthorizedUsers ON dbo.UserAccessLevels.UserID = dbo.AuthorizedUsers.UserID
  985. WHERE (dbo.AuthorizedUsers.UserAlias = @User ) AND (dbo.AuthorizedUsers.UserDomain = @Domain) AND
  986. ((AccessLevelID = 0) OR (AccessLevelID = @Level AND (AuthorizedUsers.DateSignedDCP IS Not Null AND
  987. CURRENT_TIMESTAMP <= DATEADD(month,12,DateSignedDCP) ) ) )
  988. ORDER BY AccessLevelID DESC
  989. -- @target will point to the last value we read
  990. if @target IS NULL
  991. begin
  992. select @target = 0
  993. end
  994. else
  995. begin
  996. -- @target is 0 if access is denied, we return 2 in this case
  997. if @target = 0
  998. begin
  999. select @target = 2
  1000. end
  1001. else
  1002. begin
  1003. select @target = 1
  1004. end
  1005. end
  1006. -- PRINT @target
  1007. RETURN @target
  1008. GO
  1009. SET QUOTED_IDENTIFIER OFF
  1010. GO
  1011. SET ANSI_NULLS ON
  1012. GO
  1013. SET QUOTED_IDENTIFIER OFF
  1014. GO
  1015. SET ANSI_NULLS ON
  1016. GO
  1017. -- return values:
  1018. -- 0 = user not in database
  1019. -- 1 = user in database or approved in approvals table
  1020. -- 2 = user access is denied
  1021. -- 3 = user in table, but not yet approved
  1022. -- 4 = user not in approvals table
  1023. CREATE PROCEDURE CheckUserAccessApprovals
  1024. @User varchar(50),
  1025. @Domain varchar(50),
  1026. @Level int,
  1027. @ApprovalTypeID int
  1028. As
  1029. set nocount on
  1030. -- local var to hold record id
  1031. declare @target int
  1032. declare @HasApproval int
  1033. declare @DBUserID int
  1034. declare @ApproverUserID int
  1035. declare @Reason varchar(255)
  1036. declare @DateApproved varchar(50)
  1037. declare @ApprovalsUserID int
  1038. SELECT @DBUserID=AuthorizedUsers.UserID, @target=UserAccessLevels.AccessLevelID,
  1039. @HasApproval= Approvals.UserID
  1040. FROM UserAccessLevels INNER JOIN
  1041. AuthorizedUsers ON
  1042. UserAccessLevels.UserID = AuthorizedUsers.UserID LEFT OUTER
  1043. JOIN
  1044. Approvals ON
  1045. AuthorizedUsers.UserID = Approvals.UserID
  1046. WHERE (AuthorizedUsers.UserAlias = @User) AND
  1047. (AuthorizedUsers.UserDomain = @Domain) AND
  1048. ((UserAccessLevels.AccessLevelID = 0) OR
  1049. ((UserAccessLevels.AccessLevelID = @Level) AND
  1050. (AuthorizedUsers.DateSignedDCP IS NOT NULL) AND
  1051. (CURRENT_TIMESTAMP <= DATEADD(month, 12,
  1052. AuthorizedUsers.DateSignedDCP))))
  1053. ORDER BY UserAccessLevels.AccessLevelID DESC
  1054. -- @target will point to the last value we read
  1055. if @target IS NULL
  1056. begin
  1057. select @target = 0
  1058. return @target
  1059. end
  1060. else
  1061. begin
  1062. -- @target is 0 if access is denied, we return 2 in this case
  1063. if @target = 0
  1064. begin
  1065. select @target = 2
  1066. return @target
  1067. end
  1068. end
  1069. SELECT @ApprovalsUserID = UserID,@ApproverUserID = ApproverUserID, @Reason = Reason, @DateApproved = DateApproved
  1070. FROM Approvals
  1071. WHERE UserID = @DBUserID AND ApprovalTypeID = @ApprovalTypeID
  1072. if @ApprovalsUserID is NULL
  1073. begin
  1074. -- print 'okay to pass'
  1075. -- not in this table, okay to pass if ApprovalTypeID=1
  1076. if @ApprovalTypeID=1
  1077. begin
  1078. select @target = 1
  1079. return @target
  1080. end
  1081. else
  1082. -- Otherwise, return 4
  1083. begin
  1084. select @target = 4
  1085. return @target
  1086. end
  1087. end
  1088. if @ApproverUserID is NULL or @Reason is NULL or @DateApproved is NULL
  1089. begin
  1090. -- print 'not approved yet'
  1091. select @target = 3
  1092. return @target
  1093. end
  1094. -- print 'approved'
  1095. select @target = 1
  1096. RETURN @target
  1097. GO
  1098. SET QUOTED_IDENTIFIER OFF
  1099. GO
  1100. SET ANSI_NULLS ON
  1101. GO
  1102. SET QUOTED_IDENTIFIER ON
  1103. GO
  1104. SET ANSI_NULLS ON
  1105. GO
  1106. CREATE PROCEDURE dbo.DefragIndexes
  1107. @maxfrag decimal = 80.0 --Scan density < than this number
  1108. , @ActualCountRatio int = 10 --The Actial count ratio > than this number
  1109. , @ReportOnly char(1) = 'Y' --Change to 'Y' if you just want the report
  1110. AS
  1111. SET NOCOUNT ON
  1112. DECLARE @tablename varchar (128)
  1113. , @execstr varchar (255)
  1114. , @objectid int
  1115. , @LogicalFrag decimal
  1116. , @ScanDensity decimal
  1117. , @ratio varchar (25)
  1118. , @IndexName varchar(64)
  1119. , @StartTime datetime
  1120. , @EndTime datetime
  1121. PRINT '/*****************************************************************************************************/'
  1122. PRINT 'Starting DEFRAG on ' + db_name()
  1123. SELECT @StartTime = getdate()
  1124. PRINT 'Start Time: ' + convert(varchar(20),@StartTime)
  1125. -- Declare cursor
  1126. DECLARE tables CURSOR FOR
  1127. SELECT name FROM sysobjects
  1128. WHERE type = 'u' --Tables only
  1129. --and uid = 1 --dbo only
  1130. ORDER BY name
  1131. -- Create the table
  1132. CREATE TABLE #fraglist (
  1133. ObjectName varchar(255)
  1134. , ObjectId int
  1135. , IndexName varchar(255)
  1136. , IndexId int
  1137. , Lvl int
  1138. , CountPages int
  1139. , CountRows int
  1140. , MinRecSize int
  1141. , MaxRecSize int
  1142. , AvgRecSize int
  1143. , ForRecCount int
  1144. , Extents int
  1145. , ExtentSwitches int
  1146. , AvgFreeBytes int
  1147. , AvgPageDensity int
  1148. , ScanDensity decimal
  1149. , BestCount int
  1150. , ActualCount int
  1151. , LogicalFrag decimal
  1152. , ExtentFrag decimal)
  1153. -- Open the cursor & Loop through all the tables in the database
  1154. OPEN tables FETCH NEXT
  1155. FROM tables
  1156. INTO @tablename
  1157. WHILE @@FETCH_STATUS = 0
  1158. BEGIN
  1159. -- Do the showcontig of all indexes of the table
  1160. INSERT INTO #fraglist
  1161. EXEC ('DBCC SHOWCONTIG (''' + @tablename + ''') WITH FAST, TABLERESULTS, NO_INFOMSGS')
  1162. FETCH NEXT
  1163. FROM tables INTO @tablename
  1164. END
  1165. -- Close and deallocate the cursor
  1166. CLOSE tables
  1167. DEALLOCATE tables
  1168. --Print SHOW_CONTIG Results if 'Y'
  1169. PRINT 'Fragmentation Before DBCC INDEXDEFRAG'
  1170. SELECT left(ObjectName,35) 'TableName'
  1171. ,left(IndexName,45) 'IndexName'
  1172. ,LogicalFrag 'Logical Frag %'
  1173. ,ScanDensity 'Scan Density %'
  1174. , '[' + rtrim(convert(varchar(8),BestCount)) + ':' + rtrim(convert(varchar(8),ActualCount)) + ']' 'Ratio'
  1175. FROM #fraglist
  1176. WHERE ScanDensity <= @maxfrag
  1177. AND ActualCount >= @ActualCountRatio
  1178. AND INDEXPROPERTY (ObjectId, IndexName, 'IndexDepth') > 0
  1179. -- Declare cursor for list of indexes to be defragged
  1180. DECLARE indexes CURSOR FOR
  1181. SELECT ObjectName,
  1182. ObjectId,
  1183. IndexName,
  1184. LogicalFrag,
  1185. ScanDensity,
  1186. '[' + rtrim(convert(varchar(8),BestCount)) + ':' + rtrim(convert(varchar(8),ActualCount)) + ']'
  1187. FROM #fraglist
  1188. WHERE ScanDensity <= @maxfrag
  1189. AND ActualCount >= @ActualCountRatio
  1190. AND INDEXPROPERTY (ObjectId, IndexName, 'IndexDepth') > 0
  1191. -- Open the cursor
  1192. OPEN indexes FETCH NEXT FROM indexes INTO @tablename, @objectid, @IndexName, @LogicalFrag, @ScanDensity, @ratio
  1193. WHILE @@FETCH_STATUS = 0
  1194. BEGIN
  1195. PRINT '--Executing DBCC INDEXDEFRAG (0, ' + RTRIM(@tablename) + ','
  1196. + RTRIM(@IndexName) + ') - Logical Frag Currently:'
  1197. + RTRIM(CONVERT(varchar(15),@LogicalFrag)) + '% - Scan Density Currently:' + @ratio
  1198. SELECT @execstr = 'DBCC INDEXDEFRAG (0, ''' + RTRIM(@tablename)
  1199. + ''', ''' + RTRIM(@IndexName) + ''')'
  1200. If @ReportOnly = 'Y'
  1201. BEGIN
  1202. EXEC (@execstr) --Report and Execute the DBCC INDEXDEFRAG
  1203. END
  1204. ELSE
  1205. BEGIN
  1206. Print @execstr --Report Only
  1207. END
  1208. FETCH NEXT FROM indexes INTO @tablename, @objectid, @IndexName, @LogicalFrag, @ScanDensity, @ratio END
  1209. -- Close and deallocate the cursor
  1210. CLOSE indexes
  1211. DEALLOCATE indexes
  1212. SELECT @EndTime = getdate()
  1213. PRINT 'End Time: ' + convert(varchar(20),@EndTime)
  1214. PRINT 'Elapses Time: ' + convert(varchar(20), DATEDIFF(mi, @StartTime, @EndTime) ) + ' minutes.'
  1215. -- Delete the temporary table
  1216. DROP TABLE #fraglist
  1217. GO
  1218. SET QUOTED_IDENTIFIER OFF
  1219. GO
  1220. SET ANSI_NULLS ON
  1221. GO
  1222. SET QUOTED_IDENTIFIER ON
  1223. GO
  1224. SET ANSI_NULLS ON
  1225. GO
  1226. CREATE PROCEDURE GetUserID
  1227. @User varchar(50),
  1228. @Domain varchar(50)
  1229. As
  1230. set nocount on
  1231. -- local var to hold record id
  1232. declare @target int
  1233. SELECT @target= UserID From AuthorizedUsers Where UserAlias = @User and UserDomain = @Domain
  1234. -- @target will point to the last value we read
  1235. if @target IS NULL
  1236. begin
  1237. select @target = -1
  1238. end
  1239. -- PRINT @target
  1240. RETURN @target
  1241. GO
  1242. SET QUOTED_IDENTIFIER OFF
  1243. GO
  1244. SET ANSI_NULLS ON
  1245. GO
  1246. SET QUOTED_IDENTIFIER OFF
  1247. GO
  1248. SET ANSI_NULLS ON
  1249. GO
  1250. CREATE PROCEDURE OcaAddUserAndLevel
  1251. @User varchar(50),
  1252. @Domain varchar(50),
  1253. @CurrTime varchar(50),
  1254. @Level int
  1255. As set nocount on
  1256. -- local var to hold record id
  1257. declare @target int
  1258. declare @levelDB int
  1259. declare @userDB int
  1260. declare @userID int
  1261. -- do we already have this FileInCab?
  1262. SELECT @userID = UserID FROM OcaAuthorizedUsers WHERE
  1263. UserAlias =@User AND UserDomain = @Domain
  1264. if @userID is NULL
  1265. begin -- not there, create new record
  1266. INSERT INTO OcaAuthorizedUsers (UserAlias, UserDomain, DateSignedDCP)
  1267. VALUES (@User, @Domain,@CurrTime)
  1268. SELECT @userID = UserID FROM OcaAuthorizedUsers WHERE
  1269. UserAlias =@User AND UserDomain = @Domain
  1270. end
  1271. else
  1272. begin -- update record with new signing date/time
  1273. UPDATE OcaAuthorizedUsers SET DateSignedDCP = @CurrTime
  1274. WHERE (UserID = @userID)
  1275. end
  1276. -- PRINT @userID
  1277. -- we don't have the userid
  1278. if @userID is NULL
  1279. begin
  1280. select @userID = 0
  1281. return @userID
  1282. end
  1283. select @levelDB = AccessLevelID FROM OcaAccessLevels WHERE
  1284. AccessLevelID = @Level
  1285. -- bad level passed in
  1286. if @levelDB is NULL
  1287. begin
  1288. select @levelDB = 0
  1289. return @levelDB
  1290. end
  1291. SELECT @target = UserAccessLevelID FROM OcaUserAccessLevels WHERE
  1292. UserID = @userID and AccessLevelID = @Level
  1293. if @target is NULL
  1294. begin -- not there, create new record
  1295. INSERT INTO OcaUserAccessLevels (UserID, AccessLevelID)
  1296. VALUES (@userID, @Level)
  1297. SELECT @target = UserAccessLevelID FROM OcaUserAccessLevels WHERE
  1298. UserID = @userID and AccessLevelID = @Level
  1299. end
  1300. if @target is NULL
  1301. begin
  1302. select @target = 0
  1303. return @target
  1304. end
  1305. -- return the UserAccessLevelID
  1306. RETURN @target
  1307. GO
  1308. SET QUOTED_IDENTIFIER OFF
  1309. GO
  1310. SET ANSI_NULLS ON
  1311. GO
  1312. SET QUOTED_IDENTIFIER OFF
  1313. GO
  1314. SET ANSI_NULLS ON
  1315. GO
  1316. CREATE PROCEDURE OcaAddUserForApproval
  1317. @ApprovalType int,
  1318. @User varchar(50),
  1319. @Domain varchar(50),
  1320. @Reason varchar(250)
  1321. As set nocount on
  1322. -- local var to hold record id
  1323. declare @target int
  1324. declare @userfound int
  1325. declare @userApproved int
  1326. SELECT @userfound = UserID FROM OcaAuthorizedUsers WHERE UserAlias = @User and UserDomain = @Domain
  1327. if @userfound is NULL
  1328. begin
  1329. select @target = -1
  1330. return @target
  1331. end
  1332. SELECT @userApproved = UserID FROM OcaApprovals WHERE UserID = @userfound
  1333. if @userApproved is NULL
  1334. begin -- not there, create new record
  1335. INSERT INTO OcaApprovals (UserID,ApprovalTypeID, Reason)
  1336. VALUES (@userfound,@ApprovalType,@Reason)
  1337. SELECT @userApproved = UserID from OcaApprovals WHERE UserID = @userfound
  1338. if @userApproved is NULL
  1339. begin
  1340. select @target = -1
  1341. return @target
  1342. end
  1343. end
  1344. select @target = @userApproved
  1345. RETURN @target
  1346. GO
  1347. SET QUOTED_IDENTIFIER OFF
  1348. GO
  1349. SET ANSI_NULLS ON
  1350. GO
  1351. SET QUOTED_IDENTIFIER OFF
  1352. GO
  1353. SET ANSI_NULLS ON
  1354. GO
  1355. CREATE PROCEDURE OcaAddUserLevel
  1356. @UserID int,
  1357. @Level int
  1358. As set nocount on
  1359. -- local var to hold record id
  1360. declare @target int
  1361. declare @levelDB int
  1362. declare @userDB int
  1363. select @userDB = UserID from OcaAuthorizedUsers WHERE
  1364. UserID = @UserID
  1365. if @userDB is NULL
  1366. begin
  1367. select @userDB = 0
  1368. return @userDB
  1369. end
  1370. select @levelDB = AccessLevelID FROM OcaAccessLevels WHERE
  1371. AccessLevelID = @Level
  1372. if @levelDB is NULL
  1373. begin
  1374. select @levelDB = 0
  1375. return @levelDB
  1376. end
  1377. SELECT @target = UserAccessLevelID FROM OcaUserAccessLevels WHERE
  1378. UserID = @UserID and AccessLevelID = @Level
  1379. if @target is NULL
  1380. begin -- not there, create new record
  1381. INSERT INTO OcaUserAccessLevels (UserID, AccessLevelID)
  1382. VALUES (@UserID, @Level)
  1383. SELECT @target = UserAccessLevelID FROM OcaUserAccessLevels WHERE
  1384. UserID = @UserID and AccessLevelID = @Level
  1385. end
  1386. RETURN @target
  1387. GO
  1388. SET QUOTED_IDENTIFIER OFF
  1389. GO
  1390. SET ANSI_NULLS ON
  1391. GO
  1392. SET QUOTED_IDENTIFIER ON
  1393. GO
  1394. SET ANSI_NULLS ON
  1395. GO
  1396. CREATE PROCEDURE OcaApproveUser
  1397. @UserID int,
  1398. @ApprovalTypeID int,
  1399. @ApproverUserID int,
  1400. @DateApproved varchar(50)
  1401. As set nocount on
  1402. -- local var to hold record id
  1403. declare @target int
  1404. declare @userfound int
  1405. declare @userApproved int
  1406. SELECT @userApproved = UserID FROM OcaApprovals WHERE UserID = @UserID and ApprovalTypeID = @ApprovalTypeID
  1407. if @userApproved is not NULL
  1408. begin -- not there, create new record
  1409. UPDATE OcaApprovals SET ApproverUserID = @ApproverUserID,
  1410. DateApproved = @DateApproved
  1411. WHERE (UserID = @UserID and ApprovalTypeID = @ApprovalTypeID)
  1412. end
  1413. select @target = 0
  1414. RETURN @target
  1415. GO
  1416. SET QUOTED_IDENTIFIER OFF
  1417. GO
  1418. SET ANSI_NULLS ON
  1419. GO
  1420. SET QUOTED_IDENTIFIER OFF
  1421. GO
  1422. SET ANSI_NULLS ON
  1423. GO
  1424. CREATE PROCEDURE OcaCheckUserAccess
  1425. @User varchar(50),
  1426. @Domain varchar(50),
  1427. @Level int
  1428. As set nocount on
  1429. -- local var to hold record id
  1430. declare @target int
  1431. SELECT @target= dbo.OcaUserAccessLevels.AccessLevelID FROM
  1432. dbo.OcaUserAccessLevels INNER JOIN dbo.OcaAuthorizedUsers ON dbo.OcaUserAccessLevels.UserID = dbo.OcaAuthorizedUsers.UserID
  1433. WHERE (dbo.OcaAuthorizedUsers.UserAlias = @User ) AND (dbo.OcaAuthorizedUsers.UserDomain = @Domain) AND
  1434. ((AccessLevelID = 0) OR (AccessLevelID = @Level AND (OcaAuthorizedUsers.DateSignedDCP IS Not Null AND
  1435. CURRENT_TIMESTAMP <= DATEADD(month,12,DateSignedDCP) ) ) )
  1436. ORDER BY AccessLevelID DESC
  1437. -- @target will point to the last value we read
  1438. if @target IS NULL
  1439. begin
  1440. select @target = 0
  1441. end
  1442. else
  1443. begin
  1444. -- @target is 0 if access is denied, we return 2 in this case
  1445. if @target = 0
  1446. begin
  1447. select @target = 2
  1448. end
  1449. else
  1450. begin
  1451. select @target = 1
  1452. end
  1453. end
  1454. -- PRINT @target
  1455. RETURN @target
  1456. GO
  1457. SET QUOTED_IDENTIFIER OFF
  1458. GO
  1459. SET ANSI_NULLS ON
  1460. GO
  1461. SET QUOTED_IDENTIFIER OFF
  1462. GO
  1463. SET ANSI_NULLS ON
  1464. GO
  1465. -- return values:
  1466. -- 0 = user not in database
  1467. -- 1 = user in database or approved in approvals table
  1468. -- 2 = user access is denied
  1469. -- 3 = user not yet approved
  1470. CREATE PROCEDURE OcaCheckUserAccessApprovals
  1471. @User varchar(50),
  1472. @Domain varchar(50),
  1473. @Level int,
  1474. @ApprovalTypeID int
  1475. As set nocount on
  1476. -- local var to hold record id
  1477. declare @target int
  1478. declare @HasApproval int
  1479. declare @DBUserID int
  1480. declare @ApproverUserID int
  1481. declare @Reason varchar(255)
  1482. declare @DateApproved varchar(50)
  1483. declare @ApprovalsUserID int
  1484. SELECT @DBUserID=OcaAuthorizedUsers.UserID, @target=OcaUserAccessLevels.AccessLevelID,
  1485. @HasApproval= OcaApprovals.UserID
  1486. FROM OcaUserAccessLevels INNER JOIN
  1487. OcaAuthorizedUsers ON
  1488. OcaUserAccessLevels.UserID = OcaAuthorizedUsers.UserID LEFT OUTER
  1489. JOIN
  1490. OcaApprovals ON
  1491. OcaAuthorizedUsers.UserID = OcaApprovals.UserID
  1492. WHERE (OcaAuthorizedUsers.UserAlias = @User) AND
  1493. (OcaAuthorizedUsers.UserDomain = @Domain) AND
  1494. ((OcaUserAccessLevels.AccessLevelID = 0) OR
  1495. ((OcaUserAccessLevels.AccessLevelID = @Level) AND
  1496. (OcaAuthorizedUsers.DateSignedDCP IS NOT NULL) AND
  1497. (CURRENT_TIMESTAMP <= DATEADD(month, 12,
  1498. OcaAuthorizedUsers.DateSignedDCP))))
  1499. ORDER BY OcaUserAccessLevels.AccessLevelID DESC
  1500. -- @target will point to the last value we read
  1501. if @target IS NULL
  1502. begin
  1503. select @target = 0
  1504. return @target
  1505. end
  1506. else
  1507. begin
  1508. -- @target is 0 if access is denied, we return 2 in this case
  1509. if @target = 0
  1510. begin
  1511. select @target = 2
  1512. return @target
  1513. end
  1514. end
  1515. SELECT @ApprovalsUserID = UserID,@ApproverUserID = ApproverUserID, @Reason = Reason, @DateApproved = DateApproved
  1516. FROM OcaApprovals
  1517. WHERE UserID = @DBUserID AND ApprovalTypeID = @ApprovalTypeID
  1518. -- not in this table, okay to pass
  1519. if @ApprovalsUserID is NULL
  1520. begin
  1521. -- print 'okay to pass'
  1522. select @target = 1
  1523. return @target
  1524. end
  1525. if @ApproverUserID is NULL or @Reason is NULL or @DateApproved is NULL
  1526. begin
  1527. -- print 'not approved yet'
  1528. select @target = 3
  1529. return @target
  1530. end
  1531. -- print 'approved'
  1532. select @target = 1
  1533. RETURN @target
  1534. GO
  1535. SET QUOTED_IDENTIFIER OFF
  1536. GO
  1537. SET ANSI_NULLS ON
  1538. GO
  1539. SET QUOTED_IDENTIFIER ON
  1540. GO
  1541. SET ANSI_NULLS ON
  1542. GO
  1543. CREATE PROCEDURE OcaGetUserID
  1544. @User varchar(50),
  1545. @Domain varchar(50)
  1546. As set nocount on
  1547. -- local var to hold record id
  1548. declare @target int
  1549. SELECT @target= UserID From OcaAuthorizedUsers Where UserAlias = @User and UserDomain = @Domain
  1550. -- @target will point to the last value we read
  1551. if @target IS NULL
  1552. begin
  1553. select @target = -1
  1554. end
  1555. -- PRINT @target
  1556. RETURN @target
  1557. GO
  1558. SET QUOTED_IDENTIFIER OFF
  1559. GO
  1560. SET ANSI_NULLS ON
  1561. GO
  1562. SET QUOTED_IDENTIFIER OFF
  1563. GO
  1564. SET ANSI_NULLS ON
  1565. GO
  1566. CREATE PROCEDURE OcaRemoveUserLevel
  1567. @UserID int,
  1568. @Level int
  1569. As set nocount on
  1570. -- local var to hold record id
  1571. declare @target int
  1572. declare @levelDB int
  1573. declare @userDB int
  1574. select @userDB = UserID from OcaAuthorizedUsers WHERE
  1575. UserID = @UserID
  1576. if @userDB is NULL
  1577. begin
  1578. select @userDB = 0
  1579. return @userDB
  1580. end
  1581. select @levelDB = AccessLevelID FROM OcaAccessLevels WHERE
  1582. AccessLevelID = @Level
  1583. if @levelDB is NULL
  1584. begin
  1585. select @levelDB = 0
  1586. return @levelDB
  1587. end
  1588. SELECT @target = UserAccessLevelID FROM OcaUserAccessLevels WHERE
  1589. UserID = @UserID and AccessLevelID = @Level
  1590. if @target is not NULL
  1591. begin -- there, delete record
  1592. DELETE FROM OcaUserAccessLevels WHERE UserID = @UserID and AccessLevelID = @Level
  1593. end
  1594. RETURN @target
  1595. GO
  1596. SET QUOTED_IDENTIFIER OFF
  1597. GO
  1598. SET ANSI_NULLS ON
  1599. GO
  1600. SET QUOTED_IDENTIFIER OFF
  1601. GO
  1602. SET ANSI_NULLS ON
  1603. GO
  1604. CREATE PROCEDURE RemoveUserLevel
  1605. @UserID int,
  1606. @Level int
  1607. As
  1608. set nocount on
  1609. -- local var to hold record id
  1610. declare @target int
  1611. declare @levelDB int
  1612. declare @userDB int
  1613. select @userDB = UserID from AuthorizedUsers WHERE
  1614. UserID = @UserID
  1615. if @userDB is NULL
  1616. begin
  1617. select @userDB = 0
  1618. return @userDB
  1619. end
  1620. select @levelDB = AccessLevelID FROM AccessLevels WHERE
  1621. AccessLevelID = @Level
  1622. if @levelDB is NULL
  1623. begin
  1624. select @levelDB = 0
  1625. return @levelDB
  1626. end
  1627. SELECT @target = UserAccessLevelID FROM UserAccessLevels WHERE
  1628. UserID = @UserID and AccessLevelID = @Level
  1629. if @target is not NULL
  1630. begin -- there, delete record
  1631. DELETE FROM UserAccessLevels WHERE UserID = @UserID and AccessLevelID = @Level
  1632. end
  1633. RETURN @target
  1634. GO
  1635. SET QUOTED_IDENTIFIER OFF
  1636. GO
  1637. SET ANSI_NULLS ON
  1638. GO
  1639. SET QUOTED_IDENTIFIER OFF
  1640. GO
  1641. SET ANSI_NULLS OFF
  1642. GO
  1643. -- return values:
  1644. -- 0 = user not in database
  1645. -- 1 = user in database or approved in approvals table
  1646. -- 2 = user access is denied
  1647. -- 3 = user in table, but not yet approved
  1648. -- 4 = user not in approvals table
  1649. CREATE PROCEDURE TestProc
  1650. As
  1651. set nocount on
  1652. -- local var to hold record id
  1653. declare @target int
  1654. select @target = 52
  1655. print 'foo' + CONVERT(char(19),@target)+'\n'
  1656. print @target
  1657. print '\n'
  1658. GO
  1659. SET QUOTED_IDENTIFIER OFF
  1660. GO
  1661. SET ANSI_NULLS ON
  1662. GO
  1663. SET QUOTED_IDENTIFIER ON
  1664. GO
  1665. SET ANSI_NULLS ON
  1666. GO
  1667. CREATE PROCEDURE rolemember
  1668. @rolename sysname = NULL
  1669. AS
  1670. if @rolename is not null
  1671. begin
  1672. -- VALIDATE GIVEN NAME
  1673. if not exists (select * from sysusers where name = @rolename and issqlrole = 1)
  1674. begin
  1675. raiserror(15409, -1, -1, @rolename)
  1676. return (1)
  1677. end
  1678. -- RESULT SET FOR SINGLE ROLE
  1679. select MemberName = u.name,DbRole = g.name
  1680. from sysusers u, sysusers g, sysmembers m
  1681. where g.name = @rolename
  1682. and g.uid = m.groupuid
  1683. and g.issqlrole = 1
  1684. and u.uid = m.memberuid
  1685. order by 1, 2
  1686. end
  1687. else
  1688. begin
  1689. -- RESULT SET FOR ALL ROLES
  1690. select MemberName = u.name,DbRole = g.name
  1691. from sysusers u, sysusers g, sysmembers m
  1692. where g.uid = m.groupuid
  1693. and g.issqlrole = 1
  1694. and u.uid = m.memberuid
  1695. order by 1, 2
  1696. end
  1697. return (0) -- sp_helprolemember
  1698. GO
  1699. SET QUOTED_IDENTIFIER OFF
  1700. GO
  1701. SET ANSI_NULLS ON
  1702. GO
  1703. SET QUOTED_IDENTIFIER ON
  1704. GO
  1705. SET ANSI_NULLS ON
  1706. GO
  1707. CREATE PROCEDURE updatestats
  1708. @resample CHAR(8)='FULLSCAN'
  1709. AS
  1710. DECLARE @dbsid varbinary(85)
  1711. SELECT @dbsid = sid
  1712. FROM master.dbo.sysdatabases
  1713. WHERE name = db_name()
  1714. /*Check the user sysadmin*/
  1715. IF NOT is_srvrolemember('sysadmin') = 1 AND suser_sid() <> @dbsid
  1716. BEGIN
  1717. RAISERROR(15247,-1,-1)
  1718. RETURN (1)
  1719. END
  1720. -- required so it can update stats on on ICC/IVs
  1721. set ansi_nulls on
  1722. set quoted_identifier on
  1723. set ansi_warnings on
  1724. set ansi_padding on
  1725. set arithabort on
  1726. set concat_null_yields_null on
  1727. set numeric_roundabort off
  1728. DECLARE @exec_stmt nvarchar(540)
  1729. DECLARE @tablename sysname
  1730. DECLARE @uid smallint
  1731. DECLARE @user_name sysname
  1732. DECLARE @tablename_header varchar(267)
  1733. DECLARE ms_crs_tnames CURSOR LOCAL FAST_FORWARD READ_ONLY FOR SELECT name, uid FROM sysobjects WHERE type = 'U'
  1734. OPEN ms_crs_tnames
  1735. FETCH NEXT FROM ms_crs_tnames INTO @tablename, @uid
  1736. WHILE (@@fetch_status <> -1)
  1737. BEGIN
  1738. IF (@@fetch_status <> -2)
  1739. BEGIN
  1740. SELECT @user_name = user_name(@uid)
  1741. SELECT @tablename_header = 'Updating ' + @user_name +'.'+ RTRIM(@tablename)
  1742. PRINT @tablename_header
  1743. SELECT @exec_stmt = 'UPDATE STATISTICS ' + quotename( @user_name , '[')+'.' + quotename( @tablename, '[')
  1744. if (UPPER(@resample)='FULLSCAN') SET @exec_stmt = @exec_stmt + ' WITH fullscan'
  1745. EXEC (@exec_stmt)
  1746. END
  1747. FETCH NEXT FROM ms_crs_tnames INTO @tablename, @uid
  1748. END
  1749. PRINT ' '
  1750. PRINT ' '
  1751. raiserror(15005,-1,-1)
  1752. DEALLOCATE ms_crs_tnames
  1753. RETURN(0) -- sp_updatestats
  1754. GO
  1755. SET QUOTED_IDENTIFIER OFF
  1756. GO
  1757. SET ANSI_NULLS ON
  1758. GO