home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / Multi-Leve250338212001.psc / Security2 / cSecurity2.cls < prev   
Encoding:
Visual Basic class definition  |  2001-08-20  |  12.6 KB  |  379 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "clsSecurity"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. '/* Author: Morgan Haueisen (morganh@hartcom.net)
  15. '/* Copyright (c) 2000-2001
  16.  
  17. '/* This class is the server for the client forms and modules
  18. '/* that maintain user and group security.
  19.  
  20. '/* cfSecurityGroups.frm
  21. '/* cfSecurityLogin.frm
  22. '/* cfSecurityManage.frm
  23. '/* modSecurity.bas
  24.  
  25. Option Explicit
  26.  
  27. '/* Password for Security file (security.pwd)
  28. Const DB_PWD As String = ";PWD=MorganH"
  29.  
  30.  
  31. Public Function ListAllGroups(ObjBox As Object, ByVal SecurityDatabasePath As String, Optional ByVal GroupName As String = "") As Variant
  32. '/* This function lists all groups defined with the current
  33. '/* security file and places values into an ListBox or ComboBox.
  34.  
  35. '/* ObjBox - ListBox or ComboBox the info will be put in.
  36. '/* SecurityDatabasePath - The path and file name of the security database file.
  37. '/* Returns:    True/False if ok.
  38.  
  39.   Dim MyDB  As Database
  40.   Dim MySet As Recordset
  41.   Dim SetIndex As Boolean, i As Integer
  42.     
  43.     On Error GoTo Error_Function
  44.     Set MyDB = Workspaces(0).OpenDatabase(SecurityDatabasePath, False, False, DB_PWD)
  45.     
  46.     ObjBox.Clear
  47.     
  48.     Set MySet = MyDB.OpenRecordset("Select * From Groups;")
  49.     If Not (MySet.EOF And MySet.BOF) Then
  50.         Do
  51.             ObjBox.AddItem MySet!GroupName
  52.             If MySet!GroupName = GroupName Then SetIndex = True
  53.             MySet.MoveNext
  54.         Loop Until MySet.EOF
  55.     End If
  56.     
  57.     MySet.Close
  58.     MyDB.Close
  59.     Set MySet = Nothing
  60.     Set MyDB = Nothing
  61.     
  62.     If SetIndex Then
  63.         For i = 0 To ObjBox.ListCount - 1
  64.             If ObjBox.List(i) = GroupName Then
  65.                 ObjBox.ListIndex = i
  66.                 Exit For
  67.             End If
  68.         Next i
  69.     End If
  70.     
  71.     ListAllGroups = True
  72.     
  73. Exit_Function:
  74.     On Local Error Resume Next
  75. Exit Function
  76.     
  77.     
  78. Error_Function:
  79.     Err.Raise Err.Number, "clsSecurity::ListAllGroups", Err.Description
  80.     ListAllGroups = False
  81.     Resume Exit_Function
  82.     
  83. End Function
  84.  
  85. Public Function ListAllGroupsPerUser(ObjBoxNotIn As Object, ObjBoxIn As Object, ByVal UserName As String, ByVal SecurityDatabasePath As String) As Boolean
  86. '/* This function will fill a listbox of all of the groups that a User
  87. '/* is not part of and a second listbox that contains the Groups
  88. '/* that the User is a member of.
  89.  
  90. '/* ObjBoxNotIn - Listbox or ComoBox that contains groups the user is not a member of.
  91. '/* ObjBoxIn - Listbox or ComoBox that contains groups the user is a member of.
  92. '/* UserName - a string that holds the name of the user.
  93. '/* SecurityDatabasePath - The path and file name of the security database file.
  94. '/* Returns:    True/False if ok.
  95.  
  96.   Dim MyDB  As Database
  97.   Dim MyGroups As Recordset
  98.   Dim MyRights As Recordset
  99.     
  100.     Set MyDB = Workspaces(0).OpenDatabase(SecurityDatabasePath, False, False, DB_PWD)
  101.     
  102.     ObjBoxIn.Clear
  103.     ObjBoxNotIn.Clear
  104.     
  105.     Set MyRights = MyDB.OpenRecordset("Select * From UserRights Where UserRights.UserName='" & UserName & "';")
  106.     Set MyGroups = MyDB.OpenRecordset("Select * From Groups;")
  107.     
  108.     If MyGroups.EOF And MyGroups.BOF Then
  109.     Else
  110.         Do
  111.             MyRights.FindFirst "[GroupName]='" & MyGroups!GroupName & "'"
  112.             If MyRights.NoMatch Then
  113.                 ObjBoxNotIn.AddItem MyGroups!GroupName
  114.             Else
  115.                 ObjBoxIn.AddItem MyGroups!GroupName
  116.             End If
  117.             MyGroups.MoveNext
  118.         Loop Until MyGroups.EOF
  119.     End If
  120.     
  121.     MyGroups.Close
  122.     MyRights.Close
  123.     MyDB.Close
  124.     Set MyGroups = Nothing
  125.     Set MyRights = Nothing
  126.     Set MyDB = Nothing
  127.     
  128. End Function
  129.  
  130. Public Function CreateUser(ByVal UserName As String, ByVal NoExp As Integer, _
  131.                             ByVal SecurityDatabasePath As String, _
  132.                             ByVal SystemID As String) As Boolean
  133. '/* This function creates a user
  134. '/* UserName - A string that holds the name of the user.
  135. '/* Password - A string that holds the user's password.
  136. '/* SystemID - A string that holds the applications ID. This prevents the
  137. '/*            security file from being copied from one app to another to gain access
  138. '/* SecurityDatabasePath - The path and file name of the security database file.
  139. '/* Returns:    True/False if ok.
  140.     
  141.   Dim MyDB  As Database
  142.   Dim MySet As Recordset
  143.     
  144.     On Error GoTo Error_Function
  145.     
  146.     Set MyDB = Workspaces(0).OpenDatabase(SecurityDatabasePath, False, False, DB_PWD)
  147.     
  148.     Set MySet = MyDB.OpenRecordset("Select * From Users Where [UserName] = '" & UserName & "'")
  149.     If MySet.EOF And MySet.BOF Then
  150.         MySet.AddNew
  151.         MySet!UserName = UserName
  152.         MySet!SystemID = SystemID
  153.         MySet!Date = Date
  154.         MySet!NoExp = CBool(NoExp)
  155.         MySet!UserPassword = "password"
  156.         MsgBox "The default password has been set to the word 'password'.", vbInformation
  157.         MySet.Update
  158.         CreateUser = True
  159.     Else
  160.         CreateUser = False
  161.     End If
  162.     
  163.     MySet.Close
  164.     MyDB.Close
  165.     Set MySet = Nothing
  166.     Set MyDB = Nothing
  167.     
  168.     
  169. Exit_Function:
  170.     On Error Resume Next
  171. Exit Function
  172.     
  173. Error_Function:
  174.     Err.Raise Err.Number, "clsSecurity::CreateUser", Err.Description
  175.     CreateUser = False
  176.     Resume Exit_Function
  177.    
  178. End Function
  179. Public Function EditUser(ByVal UserOldName As String, ByVal UserNewName As String, ByVal NoExp As Integer, _
  180.                             ByVal SecurityDatabasePath As String, _
  181.                             ByVal SystemID As String) As Boolean
  182. '/* This function creates a user
  183. '/* UserOldName - A string that holds the name of the selected user.
  184. '/* UserNewName - A string that holds the new name of the selected user.
  185. '/* NoExp    - A integer that determins if a user's password expires.
  186. '/* SystemID - A string that holds the applications ID. This prevents the
  187. '/*            security file from being copied from one app to another to gain access
  188. '/* SecurityDatabasePath - The path and file name of the security database file.
  189. '/* Returns:    True/False if ok.
  190.     
  191.   Dim MyDB  As Database
  192.   Dim MySet As Recordset
  193.     
  194.     On Error GoTo Error_Function
  195.     
  196.     Set MyDB = Workspaces(0).OpenDatabase(SecurityDatabasePath, False, False, DB_PWD)
  197.     If UserNewName = vbNullString Then UserNewName = UserOldName
  198.     
  199.     Set MySet = MyDB.OpenRecordset("Select * From Users Where [UserName] = '" & UserOldName & "'")
  200.     If Not (MySet.EOF And MySet.BOF) Then
  201.         MySet.Edit
  202.         MySet!UserName = UserNewName
  203.         MySet!NoExp = CBool(NoExp)
  204.         MySet.Update
  205.         EditUser = True
  206.     Else
  207.         EditUser = False
  208.     End If
  209.     
  210.     MySet.Close
  211.     
  212.     If UserOldName <> UserNewName Then
  213.         MyDB.Execute "UPDATE UserRights SET UserRights.UserName = '" & UserNewName & "' WHERE (((UserRights.UserName)='" & UserOldName & "'));"
  214.     End If
  215.     
  216.     MyDB.Close
  217.     Set MySet = Nothing
  218.     Set MyDB = Nothing
  219.     
  220.     
  221. Exit_Function:
  222.     On Error Resume Next
  223. Exit Function
  224.     
  225. Error_Function:
  226.     Err.Raise Err.Number, "clsSecurity::EditUser", Err.Description
  227.     EditUser = False
  228.     Resume Exit_Function
  229.    
  230. End Function
  231.  
  232. Public Function CreateGroup(ByVal NewGroupName As String, ByVal SecurityDatabasePath As String, Optional ByVal GroupName As String = "") As Boolean
  233. '/* This function creates a user
  234. '/* GroupName - A string that holds the name of the user.
  235. '/* SecurityDatabasePath - The path and file name of the security database file.
  236. '/* Returns:    True/False if ok.
  237.     
  238.   Dim MyDB  As Database
  239.   Dim MySet As Recordset
  240.     
  241.     On Error GoTo Error_Function
  242.     
  243.     Set MyDB = Workspaces(0).OpenDatabase(SecurityDatabasePath, False, False, DB_PWD)
  244.     
  245.     If GroupName > vbNullString Then '/* Edit Group Name
  246.         Set MySet = MyDB.OpenRecordset("Select Groups.* From Groups Where [GroupName] = '" & NewGroupName & "'")
  247.         If MySet.EOF And MySet.BOF Then
  248.             MySet.Close
  249.             Set MySet = MyDB.OpenRecordset("Select Groups.* From Groups Where [GroupName] = '" & GroupName & "'")
  250.             If Not (MySet.EOF And MySet.BOF) Then
  251.                 MySet.Edit
  252.                 MySet!GroupName = NewGroupName
  253.                 MySet.Update
  254.                 CreateGroup = True
  255.             End If
  256.             
  257.             MySet.Close
  258.             MyDB.Execute "UPDATE UserRights SET UserRights.GroupName ='" & NewGroupName & _
  259.                             "' WHERE UserRights.GroupName='" & GroupName & "';"
  260.             
  261.         Else
  262.             MySet.Close
  263.             CreateGroup = False
  264.         End If
  265.     Else '/* Add a Group Name
  266.         Set MySet = MyDB.OpenRecordset("Select Groups.* From Groups Where [GroupName] = '" & NewGroupName & "'")
  267.         If MySet.EOF And MySet.BOF Then
  268.             MySet.AddNew
  269.             MySet!GroupName = NewGroupName
  270.             MySet.Update
  271.             CreateGroup = True
  272.         Else
  273.             CreateGroup = False
  274.         End If
  275.         MySet.Close
  276.     End If
  277.     
  278.     MyDB.Close
  279.     Set MySet = Nothing
  280.     Set MyDB = Nothing
  281.     
  282.     
  283. Exit_Function:
  284.     On Error Resume Next
  285. Exit Function
  286.     
  287. Error_Function:
  288.     Err.Raise Err.Number, "clsSecurity::CreateUser", Err.Description
  289.     CreateGroup = False
  290. Resume Exit_Function
  291.    
  292. End Function
  293.  
  294.  
  295. Public Function AddUserToGroup(ByVal GroupName As String, ByVal UserName As String, ByVal SecurityDatabasePath As String) As Boolean
  296. '/* This function adds a user to a group.
  297. '/* UserName - A string that holds the name of the user.
  298. '/* GroupName - A string that holds the group name.
  299. '/* SecurityDatabasePath - The path and file name of the security database file.
  300. '/* Returns:    True/False if ok.
  301.  
  302.   Dim MyDB  As Database
  303.   Dim MySet As Recordset
  304.     
  305.     On Error GoTo Error_Function
  306.     Set MyDB = Workspaces(0).OpenDatabase(SecurityDatabasePath, False, False, DB_PWD)
  307.     
  308.     Set MySet = MyDB.OpenRecordset("Select * From UserRights Where [GroupName] = '" & GroupName & "'")
  309.     MySet.FindFirst "[UserName] = '" & UserName & "'"
  310.     If MySet.NoMatch Then
  311.         MySet.AddNew
  312.         MySet!UserName = UserName
  313.         MySet!GroupName = GroupName
  314.         MySet.Update
  315.         AddUserToGroup = True
  316.     End If
  317.     MySet.Close
  318.     MyDB.Close
  319.     Set MySet = Nothing
  320.     Set MyDB = Nothing
  321.     
  322. Exit_Function:
  323.     On Error Resume Next
  324. Exit Function
  325.     
  326.     
  327. Error_Function:
  328.     Err.Raise Err.Number, "clsSecurity::AddUserToGroup", Err.Description
  329.     Resume Exit_Function
  330. End Function
  331.  
  332. Public Function Login(ByVal UserName As String, ByVal Password As String, _
  333.                         ByVal SecurityDatabasePath As String, _
  334.                         ByVal SystemID As String) As Integer
  335. '/* This function will login to the database and the security file.
  336. '/* UserName - A string that holds the name of the user.
  337. '/* Password - A string that holds the user's password.
  338. '/* SystemID - A string that holds the applications ID. This prevents the
  339. '/*            security file from being copied from one app to another to gain access.
  340. '/* SecurityDatabasePath - The path and file name of the security database file.
  341. '/* Returns:    True or False
  342.     
  343.   Dim MyDB     As Database
  344.   Dim MySet    As Recordset
  345.   Dim ExpDays  As Byte
  346.   Dim PassDiff As Integer
  347.   Dim NoExp    As Boolean
  348.     
  349.     On Error GoTo Error_Function
  350.     
  351.     Set MyDB = Workspaces(0).OpenDatabase(SecurityDatabasePath, False, False, DB_PWD)
  352.     
  353.     Set MySet = MyDB.OpenRecordset("Select ExpDays.Days from ExpDays")
  354.     ExpDays = MySet!Days
  355.     MySet.Close
  356.     
  357.     Set MySet = MyDB.OpenRecordset("Select Users.* From Users Where [UserName] = '" & UserName & _
  358.                                     "' AND [SystemID]='" & SystemID & "';")
  359.                                     
  360.     
  361.     If MySet.EOF And MySet.BOF Then
  362.         '/* No user ID found
  363.         Login = 0
  364.     Else
  365.         '/* if the user's password is password then ignore any password the user has typed.
  366.         If MySet!UserPassword = "password" Then Password = "password"
  367.         If MySet!UserPassword <> Password Then
  368.             Login = 0
  369.         Else
  370.             '/* The login was su    MSet.E uhe logintVlu= 0
  371.         Else
  372.             '/* The login was su    MSet.E uhe alse
  373.   >pbasePath, False, False, DB_PWD)
  374.     
  375.     Set MySet = MyDB.OpenRecordset("Select-(_.* FrFalin = 0
  376.    yo>Namtiet = MyDB.OpenRecordseeateUser", Err.Description
  377.     CreateyDByDByDBg
  378.     Cre  Login = 0
  379.         C;e fiSet