/[php-vas]/trunk/example1.php
ViewVC logotype

Contents of /trunk/example1.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2 - (show annotations)
Wed Mar 14 20:14:47 2007 UTC (2 years, 8 months ago) by rbateman
File size: 3460 byte(s)
First round of file commits. The whole can be copied to a fresh directory,
bootstrapped, configured and made. make install probably doesn't work yet--I
haven't tried it.

1 <?php
2
3 require "vas.php";
4 //
5 // This is a simple demonstration program that illustrates
6 // querying Active Directory for common information.
7 //
8
9 //
10 // Constants for later....
11 //
12 $username = "jdoe";
13 $password = "jdoepassword";
14 $attrs_val2 = "John fool. Doe";
15 $uri = "DC://";
16 $scope = "sub";
17 $search_base = "CN=Users,DC=EXAMPLE,DC=COM";
18 $search_filter = "(&(ObjectClass=User)(uidNumber=*))";
19
20 $vas = new CVAS();
21
22 $vas->authenticate($username, $password);
23 //
24 // See if we authenticated correctly.
25 //
26 if ($vas->get_error_code() != VAS_ERR_SUCCESS)
27 {
28 echo "Authentication of $username failed.\n";
29 }
30
31 //
32 // See if our user is in the "Test Users" group.
33 //
34 echo "Is $username in Test Users:\n";
35
36 $group = "CN=Test Users,DC=example,DC=com";
37
38 $x = $vas->is_user_in_group("jdoe", $group);
39
40 if ($x == VAS_ERR_SUCCESS)
41 {
42 echo " Yes, $username found in group\n";
43 }
44 else
45 {
46 if ($x == VAS_ERR_NOT_FOUND)
47 {
48 echo " Not Found\n";
49 }
50 else
51 {
52 echo " ERROR\nvas_err is ", $x, " ", $vas->get_error_string(), "\n";
53 }
54 }
55
56 //
57 // Get the list of groups for our user.
58 //
59 $groups = $vas->get_groups_for_user($username, 0);
60 if ($vas->get_error_code() == VAS_ERR_SUCCESS)
61 {
62 echo "Groups for ", $username, " are:\n";
63 foreach ($groups as $g)
64 {
65 echo " ", $g, "\n";
66 }
67 }
68 else
69 {
70 printf("Failure to find attributes. Code: %s\n", $vas->get_error_string());
71 }
72
73 //
74 // Get the list of groups for the Administrator.
75 //
76 $groups = $vas->get_groups_for_user("Administrator", 0);
77 if ($vas->get_error_code() == VAS_ERR_SUCCESS)
78 {
79 echo "Groups for ", "Administrator", " are:\n";
80 foreach ($groups as $g)
81 {
82 echo " ", $g, "\n";
83 }
84 }
85 else
86 {
87 printf("Failure to find attributes. Code: %s\n", $vas->get_error_string());
88 }
89
90 //
91 // Find all the the attributes for $username that match homePhone.
92 //
93 $attrs = $vas->find( $uri, $scope, $search_base, $search_filter, "homePhone" );
94
95 if ($vas->get_error_code() == VAS_ERR_SUCCESS)
96 {
97 echo "Attributes for $username are:\n";
98 foreach ($attrs as $a)
99 {
100 echo " ", $a, "\n";
101 }
102 }
103 else
104 {
105 printf("Failure to find attributes. Code: %s\n", $vas->get_error_string());
106 }
107
108 //
109 // Find all the the attributes for Test Users
110 //
111 $attrs = $vas->get_group_attributes("Test Users", array("name", "description", "mail") );
112
113 if ($vas->get_error_code() == VAS_ERR_SUCCESS)
114 {
115 echo "Attributes for Test Users are:\n";
116 foreach ($attrs as $a)
117 {
118 echo " ", $a, "\n";
119 }
120 }
121 else
122 {
123 printf("Failure to find attributes. Code: %s\n", $vas->get_error_string());
124 }
125
126 //
127 // Add and then Delete the company attribute for the test user.
128 //
129
130 // do an extra delete incase there is one already.
131 $attrs = $vas->set_attributes("DC://", "CN=$attrs_val2,$search_base",
132 new CVAS_LDAPMod(LDAP_MOD_DELETE, "company"));
133
134 $m = new CVAS_LDAPMod(LDAP_MOD_ADD, "company");
135 $m->add_value("this is the company name");
136 $attrs = $vas->set_attributes("DC://", "CN=$attrs_val2,$search_base", $m);
137
138 if ($vas->get_error_code() == VAS_ERR_SUCCESS)
139 {
140 printf("Company attribute for %s added\n", $attrs_val2);
141 }
142 else
143 {
144 printf("Failure to add attribute. Code: %s\n", $vas->get_error_string());
145 }
146
147 $attrs = $vas->set_attributes("DC://", "CN=$attrs_val2,$search_base",
148 new CVAS_LDAPMod(LDAP_MOD_DELETE, "company"));
149
150 if ($vas->get_error_code() == VAS_ERR_SUCCESS)
151 {
152 printf("Company attribute for %s deleted\n", $attrs_val2);
153 }
154 else
155 {
156 printf("Failure to delete attribute. Code: %s\n", $vas->get_error_string());
157 }
158
159 ?>

Ted.Percival@quest.com
ViewVC Help
Powered by ViewVC 1.1.2