composer package updates
[openemr.git] / vendor / adldap2 / adldap2 / docs / models / group.md
blobc597188f36814ac3b20801976244bf67033c2c72
1 # The Group Model
3 > **Note**: This model contains the trait `HasMemberOf`.
4 > For more information, visit the documentation:
6 > [HasMemberOf](traits/has-member-of.md)
8 ## Creation
10 ```php
11 // Adldap\Models\Group
12 $group = $provider->make()->group([
13     'cn' => 'Managers',
14 ]);
16 // Create group's DN through the DN Builder:
17 $group = $provider->make()->group();
19 $dn = $group->getDnBuilder();
21 $dn->addOu('Workstation Computers');
23 $dn->addCn("Managers");
25 $group->setDn($dn);
27 // Or set the DN manually:
28 $ou->setDn('cn=Managers,ou=Workstation Computers,dc=test,dc=local,dc=com');
30 $group->save();
31 ```
33 ## Getting a groups members
35 When you receive a `Group` model instance, it will contain a `member`
36 attribute which contains the distinguished names of all
37 the members inside the group.
39 ```php
40 $group = $provider->search()->groups()->first();
42 foreach ($group->members as $member) {
44     // 'cn=John Doe,dc=corp,dc=acme,dc=org'
45     echo $member;
48 ```
50 But this might not be useful, since we might actually want the models for each member.
52 This can be easily done with the `getMembers()` method on the group.
54 ```php
55 $group = $provider->search()->groups()->first();
57 foreach ($group->getMembers() as $member) {
59     // Will be an instance of a Adldap `Model`
60     $member->getCommonName();
63 ```
65 You should be aware however, that calling the `getMembers()` method will
66 query your `AD` server for **every** member contained in
67 the group to retrieve its model.
69 Think of this example below as what is being called behind the scenes:
71 ```php
72 $group = $provider->search()->groups()->first();
74 foreach ($group->members as $member) {
76     $model = $provider->search()->findByDn($member);
79 ```
81 ### Paginating Group members
83 The group you're looking for might contain hundreds / thousands of members.
85 In this case, your server might only return you a portion of the groups members.
87 To get around this limit, you need to ask your server to paginate the groups members through a select:
89 ```php
90 $group = $provider->search()->groups()->select('member;range=0-500')->first();
92 foreach ($group->members as $member) {
93     // We'll only have 500 members in this query.
95 ```
97 Now, when we have the group instance, we'll only have the first `500` members inside this group. However, calling the `getMembers()` method will automatically retrieve the rest of the members for you:
99 ```php
100 $group = $provider->search()->groups()->select('member;range=0-500')->first();
102 foreach ($group->getMembers() as $member) {
103     
104     // Adldap will automatically retrieve the next 500 records until it's retrieved all records.
105     
106     $member->getCommonName();
107     
111 ## Getting only a groups member names
113 To retrieve only the names of the members contained in a group, call the `getMemberNames()` method:
115 ```php
116 foreach ($group->getMemberNames() as $name) {
118     // Returns 'John Doe' 
119     echo $name;
124 > **Note**: This method does not query your server for each member to retrieve its name. It
125 > only parses the distinguished names from the groups `member` attribute. This means that
126 > if you have paginated group members, you will need to perform another query yourself
127 > to retrieve the rest of the member names (or just call the `getMembers()` method).
129 ## Setting Group Members
131 To set members that are apart of the group, you can perform this in two ways:
133 > **Note**: Remember, this will remove **all** pre-existing members, and set the new given members on the group.
135 ```php
136 $members = [
137     'cn=John Doe,dc=corp,dc=acme,dc=org',
138     'cn=Jane Doe,dc=corp,dc=acme,dc=org',
141 $group->setMembers($members);
143 $group->save();
146 Or manually:
148 ```php
149 $group->member = [
150     'cn=John Doe,dc=corp,dc=acme,dc=org',
151     'cn=Jane Doe,dc=corp,dc=acme,dc=org',
154 $group->save();
157 ## Adding One Member
159 To add a single member to a group, use the `addMember()` method:
161 > **Note**: You do not need to call the `save()` method after adding a
162 > member. It's automatically called so you can determine
163 > if the member was successfully added.
165 ```php
166 // We can provide a model, or just a plain DN of the new member
167 $user = $provider->search()->users()->first();
169 if ($group->addMember($user)) {
171     // User was successfully added to the group!
175 // Or
177 $user = 'cn=John Doe,dc=corp,dc=acme,dc=org';
179 if ($group->addMember($user)) {
180     //
184 ## Adding Multiple Group Members
186 To add multiple members to a group, use the `addMembers()` method:
188 > **Note**: You do not need to call the `save()` method after adding
189 > members. It's automatically called so you can determine
190 > if the members were successfully added.
192 ```php
193 $members = [
194     'cn=John Doe,dc=corp,dc=acme,dc=org',
195     'cn=Jane Doe,dc=corp,dc=acme,dc=org',
198 $group->addMembers($members);
200 // Or
202 $user = $provider->search()->users()->first();
204 if ($group->addMembers($user)) {
205     //
209 ## Removing One Member
211 To remove a single member to a group, use the `removeMember()` method:
213 ```php
214 // We can provide a model, or just a plain DN of the existing member
215 $group = $provider->search()->groups()->first();
217 $member = $group->getMembers()->first();
219 if ($group->removeMember($member)) {
221     // Member was successfully removed from the group!
225 // Or
227 $user = 'cn=John Doe,dc=corp,dc=acme,dc=org';
229 if ($group->removeMember($user)) {
230     //
234 ## Removing All Members
236 To remove all members, use the `removeMembers()` method:
238 ```php
239 if ($group->removeMembers()) {
241     // All members were successfully removed!