Issues (2)

docs/examples/all.php (1 issue)

Labels
Severity
1
<?php
2
/**
3
FreeIPA library for PHP
4
Copyright (C) 2015  Tobias Sette <[email protected]>
5
6
This program is free software: you can redistribute it and/or modify
7
it under the terms of the GNU Lesser General Public License as published by
8
the Free Software Foundation, either version 3 of the License, or
9
(at your option) any later version.
10
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
GNU Lesser General Public License for more details.
15
16
You should have received a copy of the GNU Lesser General Public License
17
along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
*/
19
20
/**
21
 * This file contains examples of utilization of this library
22
 */
23
24
ini_set( 'display_errors', 1);
25
ini_set( 'track_errors',   1);
26
ini_set( 'html_errors',    1);
27
error_reporting( E_ALL );
28
29
require_once( 'functions_utils.php' );
30
31
$host = 'ipa.demo1.freeipa.org';
32
// The certificate can be obtained in https://$host/ipa/config/ca.crt
33
$certificate = __DIR__ . "/../certs/ipa.demo1.freeipa.org_ca.crt";
34
$user = 'admin';
35
$password = 'Secret123';
36
$search = 'test';
37
$random = rand(1, 9999);
38
39
require_once('../bootstrap.php');
40
try {
41
    $ipa = new \FreeIPA\APIAccess\Main($host, $certificate);
0 ignored issues
show
The type FreeIPA\APIAccess\Main was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
42
    //$ipa->connection($host, $certificate);
43
} catch (Exception $e) {
44
    _print("[instance] Exception. Message: {$e->getMessage()} Code: {$e->getCode()}");
45
    die();
46
}
47
48
// If you wish to force the use of one specific version of API (for example:
49
// after make tests in the code and define that he does not work with different
50
// versions).
51
//$ipa->connection()->setAPIVersion('2.112');
52
53
// Make authentication
54
try {
55
    $ret_aut = $ipa->connection()->authenticate($user, $password);
56
    if (TRUE === $ret_aut) { // user is authenticate
57
        _print('Authentication successful');
58
    } else {
59
        $auth_info = $ipa->connection()->getAuthenticationInfo();
60
        var_dump($auth_info);
61
        // For more details:
62
        //$ret_curl = $ipa->connection()->getCurlError();
63
        //print "User is not authenticated. Return is: <br/>" . PHP_EOL;
64
        //print "cURL: " . $ret_curl[0] . " (" . $ret_curl[1] . ")<br/>" . PHP_EOL;
65
        //print "cURL string: " . $ipa->connection()->getCurlReturn() . "<br/>\n";
66
        die();
67
    }
68
} catch (Exception $e) {
69
    _print("[login] Exception. Message: {$e->getMessage()} Code: {$e->getCode()}");
70
    die();
71
}
72
73
74
// Make a connection test with the server
75
_print('Ping');
76
try {
77
    $ret_ping = $ipa->connection()->ping();
78
    if ($ret_ping) {
79
        _print('Done!');
80
    } else {
81
        _print('Error in ping!');
82
    }
83
} catch (Exception $e) {
84
    _print("[ping] Exception. Message: {$e->getMessage()} Code: {$e->getCode()}");
85
    die();
86
}
87
88
89
// Get User information
90
_print("Showing o user \"$user\"");
91
try {
92
    $ret_user = $ipa->user()->get($user);
93
    if (TRUE == $ret_user) {
94
        _print('User found');
95
        var_dump($ret_user);
96
    } else {
97
        _print("User $user not found");
98
    }
99
} catch (Exception $e) {
100
    _print("Message: {$e->getMessage()} Code: {$e->getCode()}");
101
    die();
102
}
103
104
105
// Searching a user
106
_print("Searching users with login/name contains contenham \"$search\"");
107
try {
108
    $ret_search_users = $ipa->user()->find(array($search));
109
    if ($ret_search_users) {
110
        $t = count($ret_search_users);
111
        print "Found $t usuários. Names: ";
112
        for ($i = 0; $i < $t; $i++) {
113
            print $ret_search_users[$i]->uid[0] . " | ";
114
        }
115
        _print();
116
    } else {
117
        _print('No users found');
118
    }
119
} catch (Exception $e) {
120
    _print("[searching user] Exception. Message: {$e->getMessage()} Code: {$e->getCode()}");
121
    die();
122
}
123
124
125
// Procura um user atraves de um campo identificador
126
// See documentarion of method \FreeIPA\APIAccess\User->findBy() !
127
_print("Searching for users with login \"$search\"");
128
try {
129
    $search_user_by = $ipa->user()->findBy('uid', 'admin'); // login
130
    //$search_user_by = $ipa->user()->findBy('mail', '[email protected]'); // email
131
    //$search_user_by = $ipa->user()->findBy('givenname', $search); // first name
132
    //$search_user_by = $ipa->user()->findBy('cn', 'Administrator'); // full name
133
    //$search_user_by = $ipa->user()->findBy('in_group', 'admins'); // user in group
134
    if ($search_user_by) {
135
        _print('Users found');
136
        var_dump($search_user_by);
137
    } else {
138
        _print('No users found');
139
    }
140
} catch (\Exception $e) {
141
    _print("[search user by] Exception. Message: {$e->getMessage()} Code: {$e->getCode()}");
142
    _print("Json request is {$ipa->connection()->getJsonRequest()}");
143
    _print("Json response is {$ipa->connection()->getJsonResponse()}");
144
    die();
145
}
146
147
148
// Insert a new user
149
$new_user_data = array(
150
    'givenname' => 'Richardi',
151
    'sn' => 'Stallman',
152
    'uid' => "stallman$random",
153
    'mail' => "[email protected]",
154
    'userpassword' => $password,
155
);
156
_print("Adding new user {$new_user_data['uid']} whith password \"$password\"");
157
try {
158
    $add_user = $ipa->user()->add($new_user_data);
159
    if ($add_user) {
160
        _print('User added');
161
    } else {
162
        _print('Error while adding user');
163
    }
164
} catch (\Exception $e) {
165
    _print("[insert new user] Message: {$e->getMessage()} Code: {$e->getCode()}");
166
    die();
167
}
168
169
170
// Changes the previously created user
171
$modify_user_data = array(
172
    'givenname' => 'Richard',
173
);
174
_print("Modifying the user {$new_user_data['uid']}");
175
try {
176
    $modify_user = $ipa->user()->modify($new_user_data['uid'], $modify_user_data);
177
    if ($modify_user) {
178
        _print('User modified');
179
    } else {
180
        _print('Error while modifying user');
181
    }
182
} catch (\Exception $e) {
183
    _print("[modifying user] Message: {$e->getMessage()} Code: {$e->getCode()}");
184
    die();
185
}
186
187
188
// Change the password for the previously created user
189
$data_change_pass = array(
190
    'userpassword' => 'linus123',
191
);
192
_print("Changing the password for user {$new_user_data['uid']} to {$data_change_pass['userpassword']}");
193
try {
194
    $change_pass = $ipa->user()->modify($new_user_data['uid'], $data_change_pass);
195
    if ($change_pass) {
196
        _print('Password changed');
197
    } else {
198
        _print('Error while changing the password');
199
    }
200
} catch (\Exception $e) {
201
    _print("[change password] Message: {$e->getMessage()} Code: {$e->getCode()}");
202
    die();
203
}
204
205
206
// Add group
207
_print("Add group \"group$random\"");
208
try {
209
    $add_group = $ipa->group()->add("group$random", "group$random description");
210
    if ($add_group) {
211
        _print('Group added');
212
    } else {
213
        _print('Error while adding a group');
214
    }
215
} catch (\Exception $e) {
216
    _print("[add group] Message: {$e->getMessage()} Code: {$e->getCode()}");
217
    die();
218
}
219
220
221
// Add user to group
222
_print("Add \"$user\" to group \"group$random\"");
223
try {
224
    $add_user_group = $ipa->group()->addMember("group$random", $user);
225
    if ($add_group) {
226
        _print('User added');
227
        var_dump($add_user_group);
228
    } else {
229
        _print('Error while adding user to group');
230
    }
231
} catch (\Exception $e) {
232
    _print("[add user to group] Message: {$e->getMessage()} Code: {$e->getCode()}");
233
    die();
234
}
235
236
237
_print('DONE');
238