Navigation:  Samples >

Groups and Users sample

Previous pageReturn to chapter overviewNext page

This example illustrates the use of the Group and User objects and the Groups and Users collections. First, it creates a new User object with the Name, PID, and Password property settings of "Pat Smith", "abc123DEF456", and "My Secret" and appends the object to the Users collection of the default Workspace object. Next, it creates a new Group object with the Name and PID property settings of "Accounting" and "UVW987xyz654" and appends the object to the Groups collection of the default Workspace object. Then the example adds user Pat Smith to the Accounting group. Finally, it enumerates the User and Group objects in the collections of the default Workspace object. See the methods and properties listed in the Group and User summary topics for additional examples.

 

 

FUNCTION Start

LOCAL oEng        AS DaodBEngine

LOCAL oWs        AS DaoWorkspace

LOCAL oUser AS DaoUser

LOCAL oGrp        AS DaoGroup

LOCAL cSysDb AS STRING

LOCAL cbOldErr AS CODEBLOCK

LOCAL oError AS USUAL

set color TO w+/b

cls

cbOldErr := ErrorBlock({|oErr|_Break(oErr)})

// Create user

oENg                := DaoDbEngine{}

cSysDb        := DaoregistrySystemDb()

IF ! Empty(cSysDb) .and. File(cSysDb)

 ? "Workgroup Information file ", cSysDb

 oEng:SystemDb := cSysDb

ELSE

 ? "No Workgroup Information file "

 wait

 quit

ENDIF

 

oWs       := oEng:WorkSpaces:[Item,1]

BEGIN SEQUENCE

 oWs:Users:Delete("Pat Smith")        

END

BEGIN SEQUENCE

 oWs:Groups:Delete("Accounting")

END

 

? "Creating account 'Path Smith'"

oUser     := oWs:CreateUser("Pat Smith","abc123DEF456", "My Secret")

BEGIN SEQUENCE

 oWs:Users:Append(oUser)

RECOVER USING oError

 Eval(cbOldErr, oError)

END

// Create group

? "Creating group 'Accounting'"

oGrp := oWs:CreateGroup("Accounting", "UVW987xyz654")

BEGIN SEQUENCE

 oWs:Groups:Append(oGrp)

 // Add user to group

 ? "Adding user 'Path Smith' to account 'Accounting'"

 oUser := oGrp:Createuser("Pat Smith",NIL,NIL)

 oGrp:Users:Append(oUser)

 

RECOVER USING oError

 Eval(cbOldErr, oError)

END

 

// Enumerate all users.

ShowUsers(oWs)

ShowGroups(oWs)

? "Removing 'Path Smith' and 'Accounting'"

oWs:Users:Delete("Pat Smith")

oWs:Groups:Delete("Accounting")

 

// Enumerate all users.

ShowUsers(oWs)

ShowGroups(oWs)

 

RETURN

 

STATIC FUNCTION ShowGroups(oWs AS DaoWorkspace)

LOCAL i,j AS DWORD

LOCAL oGrp AS DaoGroup

?

? "Show groups"

FOR i := 1 TO oWs:Groups:Count

 oGrp := oWs:Groups[i]

 ? "Enumeration of Groups: ", oGrp:Name

 // Enumerate users.

 ? "    Users: "

 FOR j := 1 TO oGrp:Users:Count

         ? Space(10),oGrp:Users[j]:Name

 NEXT

NEXT

wait

RETURN

 

STATIC FUNCTION ShowUsers(oWs AS DaoWorkspace)

LOCAL i,j AS DWORD

LOCAL oUser AS DaoUser

?

? "Show users"

FOR i := 1 TO oWs:Users:Count

 oUser := oWs:Users[i]

 ? "Enumeration of User: ", oUser:Name

 // Enumerate groups

 ? "    Group: "

 FOR j := 1 TO oUser:Groups:Count

         ? Space(10),oUser:Groups[j]:Name

 NEXT

NEXT

wait

RETURN