LDAPI
in package
LDAP Connector class
This is the main class to interact with the LDAP. Avoid using functions starting with an underscore ("_") directly, that should not be necessary.
Tags
Table of Contents
- $bind : array<string|int, mixed>|null
- Bind paths of the Astatine LDAP
- $config : array<string|int, mixed>|null
- Configuration from config file
- $connection : resource|null
- The LDAP connection used by default
- $write_enable : bool
- Write protected, enabled with valid in config
- __construct() : void
- Initialize class, create a new LDAP connection using the config set
- __destruct() : mixed
- Neatly close the LDAP connection
- _changeUserPassword() : bool
- Change the users password and save to the LDAP
- _getEmailForward() : string|false
- Return forwarding email for specific user from the forwarding table
- _getUserPhoto() : string|false
- Get the user photo
- _saveChangeUsername() : bool
- Rename an LDAP user
- _saveEmailForward() : bool
- Edit forwarding email for specific user (errors are placed under 'emailforward')
- _saveNewObject() : bool
- Save a new object to the LDAP
- _saveRenameCommittee() : bool
- Save the renaming of a committee to the LDAP
- _saveUpdatedObject() : bool
- Save a modified object to the LDAP
- _saveUserPhoto() : bool
- Save new photo to the ldap (errors are placed under 'photo')
- createCommittee() : LDAPCommittee
- Return a new committee object
- createCommitteeWithLink() : LDAPCommittee
- Return a new committee object and link it to this LDAPI instance
- createUser() : LDAPUser
- Return a new user object
- createUserWithLink() : LDAPUser
- Return a new user object with a link to this LDAPI instance
- deleteCommittee() : bool
- Remove a committee from the LDAP - Can not be undone!
- deleteUser() : bool
- Remove a user from the LDAP - Can not be undone!
- findAllCommittees() : array<string|int, LDAPCommittee>|false
- Get all committees
- findAllUsers() : array<string|int, LDAPUser>|false
- Get all active users (no old members), so type < 4
- findCommitteesByValues() : array<string|int, LDAPCommittee>|false
- Find committees based on field(s)
- findUsersByValues() : array<string|int, LDAPUser>|false
- Find users based on field(s)
- getCommittee() : LDAPCommittee|false
- Get committee based on the id
- getCommitteeByName() : LDAPCommittee|false
- Get committees by name
- getCommittees() : array<string|int, LDAPCommittee>
- Get committees based on a list of ids. Uses only a single LDAP query, so should be more efficient that calling `getCommittee()` multiple times.
- getUser() : LDAPUser|false
- Get single specified user by primary key
- getUserByStudentnumber() : LDAPUser|false
- Get user by student number
- getUserByUsername() : LDAPUser|false
- Return single user by username
- getUsers() : array<string|int, LDAPUser>
- Get a list of users by their primary key. Uses only a single LDAP query, and should be efficient than calling `getUser()` multiple times. The function cannot tell which ids failed
- login() : LDAPUser|false
- Attempt to login, return user on success
- paginate() : int|false
- Paginate the array of objects
- saveNewCommittee() : bool
- Save a new committee to the LDAP
- saveNewUser() : bool
- Save a new user to the LDAP
- saveUpdatedCommittee() : bool
- Save changes of a committee to the LDAP
- saveUpdatedUser() : bool
- Save changes of a user to the LDAP
- setLimit() : mixed
- Set size limit for this ldap instance
- _close() : void
- Shut the LDAP connection, can only be called by the destructor
- _createFilter() : string
- Create a string with an LDAP filter from an array of criteria
- _createFilterEntry() : string
- Creates a filter entry from array, transform display keys to ldap keys
- _findObjectsByValues() : array<string|int, LDAPObject>|false
- Find objects based on field(s)
- _getConfig() : array<string|int, mixed>
- Get config from file
- _getNextObjectID() : int
- Find the next incremental primary id
- _getRandomString() : string
- Produce random string from [0-9, a-z, A-Z]
- _image_resize() : string|false
- Resize an image to specified format
- _ldap_warning_handler() : mixed
- Error handler that will simply suppress some LDAP warnings that should not show on the webpage
- _makeConnection() : resource|false
- Create new LDAP connection
- _writeCheck() : bool
- Check if instance can perform writes and print error if necessary
Properties
$bind
Bind paths of the Astatine LDAP
protected
array<string|int, mixed>|null
$bind
= null
$config
Configuration from config file
protected
array<string|int, mixed>|null
$config
= null
$connection
The LDAP connection used by default
protected
resource|null
$connection
= null
$write_enable
Write protected, enabled with valid in config
protected
bool
$write_enable
= false
Methods
__construct()
Initialize class, create a new LDAP connection using the config set
public
__construct([string $config_key = 'default' ]) : void
Parameters
- $config_key : string = 'default'
Return values
void —__destruct()
Neatly close the LDAP connection
public
__destruct() : mixed
Return values
mixed —_changeUserPassword()
Change the users password and save to the LDAP
public
_changeUserPassword(LDAPUser $user, string $newPassword) : bool
Parameters
- $user : LDAPUser
- $newPassword : string
Return values
bool —_getEmailForward()
Return forwarding email for specific user from the forwarding table
public
_getEmailForward(LDAPUser $user) : string|false
Parameters
- $user : LDAPUser
Return values
string|false —_getUserPhoto()
Get the user photo
public
_getUserPhoto(LDAPUser $user, bool $thumb) : string|false
Parameters
- $user : LDAPUser
- $thumb : bool
-
True for small photo, false for large
Return values
string|false —Returns image data as string, simply echo to render
_saveChangeUsername()
Rename an LDAP user
public
_saveChangeUsername(LDAPUser $user, string $new_username) : bool
Parameters
- $user : LDAPUser
- $new_username : string
Return values
bool —_saveEmailForward()
Edit forwarding email for specific user (errors are placed under 'emailforward')
public
_saveEmailForward(LDAPUser $user, string $email) : bool
Parameters
- $user : LDAPUser
- $email : string
Return values
bool —_saveNewObject()
Save a new object to the LDAP
public
_saveNewObject(LDAPObject $object[, array<string|int, mixed>|null $additional_fields = null ]) : bool
Parameters
- $object : LDAPObject
- $additional_fields : array<string|int, mixed>|null = null
-
Additional info (with LDAP keys)
Return values
bool —_saveRenameCommittee()
Save the renaming of a committee to the LDAP
public
_saveRenameCommittee(LDAPCommittee $committee, string $new_name) : bool
Parameters
- $committee : LDAPCommittee
- $new_name : string
Return values
bool —_saveUpdatedObject()
Save a modified object to the LDAP
public
_saveUpdatedObject(LDAPObject $object) : bool
Parameters
- $object : LDAPObject
Return values
bool —_saveUserPhoto()
Save new photo to the ldap (errors are placed under 'photo')
public
_saveUserPhoto(LDAPUser $user, string $filepath) : bool
Parameters
- $user : LDAPUser
- $filepath : string
Return values
bool —createCommittee()
Return a new committee object
public
static createCommittee([array<string|int, mixed>|null $data = null ]) : LDAPCommittee
Parameters
- $data : array<string|int, mixed>|null = null
-
Information, just like in patchEntity
Return values
LDAPCommittee —createCommitteeWithLink()
Return a new committee object and link it to this LDAPI instance
public
createCommitteeWithLink([array<string|int, mixed>|null $data = null ]) : LDAPCommittee
Parameters
- $data : array<string|int, mixed>|null = null
-
Information, just like in patchEntity
Return values
LDAPCommittee —createUser()
Return a new user object
public
static createUser([array<string|int, mixed>|null $data = null ]) : LDAPUser
Parameters
- $data : array<string|int, mixed>|null = null
-
Information, like for patchEntity
Return values
LDAPUser —createUserWithLink()
Return a new user object with a link to this LDAPI instance
public
createUserWithLink([array<string|int, mixed>|null $data = null ]) : LDAPUser
Parameters
- $data : array<string|int, mixed>|null = null
Return values
LDAPUser —deleteCommittee()
Remove a committee from the LDAP - Can not be undone!
public
deleteCommittee(LDAPCommittee $committee) : bool
Parameters
- $committee : LDAPCommittee
Return values
bool —deleteUser()
Remove a user from the LDAP - Can not be undone!
public
deleteUser(LDAPUser $user) : bool
Parameters
- $user : LDAPUser
Return values
bool —findAllCommittees()
Get all committees
public
findAllCommittees() : array<string|int, LDAPCommittee>|false
Return values
array<string|int, LDAPCommittee>|false —Array of LDAPCommittee objects
findAllUsers()
Get all active users (no old members), so type < 4
public
findAllUsers() : array<string|int, LDAPUser>|false
Return values
array<string|int, LDAPUser>|false —Array of LDAPUser objects
findCommitteesByValues()
Find committees based on field(s)
public
findCommitteesByValues(array<string|int, mixed> $criteria) : array<string|int, LDAPCommittee>|false
Parameters
- $criteria : array<string|int, mixed>
-
Associated array of search values, use second level for "or"
Return values
array<string|int, LDAPCommittee>|false —Array of LDAPCommittee objects
findUsersByValues()
Find users based on field(s)
public
findUsersByValues(array<string|int, mixed> $criteria) : array<string|int, LDAPUser>|false
Parameters
- $criteria : array<string|int, mixed>
-
Associated array of criteria, use second level for "or"
Return values
array<string|int, LDAPUser>|false —Array of LDAPUser objects
getCommittee()
Get committee based on the id
public
getCommittee(string|int $id) : LDAPCommittee|false
Parameters
- $id : string|int
Return values
LDAPCommittee|false —getCommitteeByName()
Get committees by name
public
getCommitteeByName(string $name) : LDAPCommittee|false
Parameters
- $name : string
Return values
LDAPCommittee|false —getCommittees()
Get committees based on a list of ids. Uses only a single LDAP query, so should be more efficient that calling `getCommittee()` multiple times.
public
getCommittees(array<string|int, mixed> $ids) : array<string|int, LDAPCommittee>
There is failure for the function on invalid ids
Parameters
- $ids : array<string|int, mixed>
-
List of primary keys
Return values
array<string|int, LDAPCommittee> —List of committee objects
getUser()
Get single specified user by primary key
public
getUser(string|int $id) : LDAPUser|false
Parameters
- $id : string|int
Return values
LDAPUser|false —getUserByStudentnumber()
Get user by student number
public
getUserByStudentnumber(string $number) : LDAPUser|false
Parameters
- $number : string
-
Student number (no 's')
Return values
LDAPUser|false —getUserByUsername()
Return single user by username
public
getUserByUsername(string $username) : LDAPUser|false
Parameters
- $username : string
Return values
LDAPUser|false —getUsers()
Get a list of users by their primary key. Uses only a single LDAP query, and should be efficient than calling `getUser()` multiple times. The function cannot tell which ids failed
public
getUsers(array<string|int, mixed> $ids) : array<string|int, LDAPUser>
Parameters
- $ids : array<string|int, mixed>
-
List of primary ids
Return values
array<string|int, LDAPUser> —Array of ldap user objects
login()
Attempt to login, return user on success
public
login(string $username, string $password) : LDAPUser|false
Parameters
- $username : string
- $password : string
-
(clear, un-hashed)
Return values
LDAPUser|false —paginate()
Paginate the array of objects
public
paginate(array<string|int, mixed> &$list, array<string|int, mixed> $options) : int|false
This cannot be more efficiently by e.g. query modification.
The possible options are:
sort
=> Field to sort on
direction
=> Direction to sort on
limit
=> Number of entries per page
page
=> The page to return
When the last page is reached the page will be smaller than limit
.
Parameters
- $list : array<string|int, mixed>
-
Array to be paginated
- $options : array<string|int, mixed>
-
List of settings
Return values
int|false —Return number of pages or false
it no pagination was performed
saveNewCommittee()
Save a new committee to the LDAP
public
saveNewCommittee(LDAPCommittee $committee) : bool
Parameters
- $committee : LDAPCommittee
Return values
bool —saveNewUser()
Save a new user to the LDAP
public
saveNewUser(LDAPUser $user, string $password) : bool
Parameters
- $user : LDAPUser
- $password : string
-
(clear, un-hashed)
Return values
bool —saveUpdatedCommittee()
Save changes of a committee to the LDAP
public
saveUpdatedCommittee(LDAPCommittee $committee) : bool
Parameters
- $committee : LDAPCommittee
Return values
bool —saveUpdatedUser()
Save changes of a user to the LDAP
public
saveUpdatedUser(LDAPUser $user) : bool
Parameters
- $user : LDAPUser
Return values
bool —setLimit()
Set size limit for this ldap instance
public
setLimit(int|false $limit) : mixed
Persists between searches, reset to prevent this. Limiting the number of records is not an efficient way of pagination! Simple benchmarks give the impression that PHP-side pagination is faster than LDAP side.
Parameters
- $limit : int|false
-
Size limit, set to false to disable limit
Return values
mixed —_close()
Shut the LDAP connection, can only be called by the destructor
protected
_close() : void
Return values
void —_createFilter()
Create a string with an LDAP filter from an array of criteria
protected
_createFilter(array<string|int, mixed> $criteria, string $class) : string
Parameters
- $criteria : array<string|int, mixed>
-
Associative array with search criteria, use second level for "or"
- $class : string
-
Class of the looked-for object
Return values
string —String ready for ldap_search()
_createFilterEntry()
Creates a filter entry from array, transform display keys to ldap keys
protected
_createFilterEntry(string $key, string $value, string|LDAPObject $class) : string
Parameters
- $key : string
-
Property key
- $value : string
-
Criterion
- $class : string|LDAPObject
Return values
string —_findObjectsByValues()
Find objects based on field(s)
protected
_findObjectsByValues(string|LDAPObject $class[, array<string|int, mixed> $criteria = [] ][, bool $by_id = false ]) : array<string|int, LDAPObject>|false
Parameters
- $class : string|LDAPObject
-
LDAPI class to search for
- $criteria : array<string|int, mixed> = []
-
Associated array of search criteria, use second level for "or", null for returning all objects
- $by_id : bool = false
-
Set to true to create array based on ID
Return values
array<string|int, LDAPObject>|false —Array of LDAP objects
_getConfig()
Get config from file
protected
_getConfig(string $config_key) : array<string|int, mixed>
Parameters
- $config_key : string
-
Section of config to use
Return values
array<string|int, mixed> —_getNextObjectID()
Find the next incremental primary id
protected
_getNextObjectID(string $class) : int
Parameters
- $class : string
Return values
int —_getRandomString()
Produce random string from [0-9, a-z, A-Z]
protected
_getRandomString([int $size = 10 ]) : string
Parameters
- $size : int = 10
-
= 10 Number of characters
Return values
string —Random string
_image_resize()
Resize an image to specified format
protected
_image_resize(string $source, int $width, int $height) : string|false
Parameters
- $source : string
-
File path
- $width : int
-
New width in pixels
- $height : int
-
New height in pixels
Return values
string|false —Return image data in string format or false on error
_ldap_warning_handler()
Error handler that will simply suppress some LDAP warnings that should not show on the webpage
protected
_ldap_warning_handler( $errno, $errstr) : mixed
Parameters
Return values
mixed —_makeConnection()
Create new LDAP connection
protected
_makeConnection() : resource|false
Return values
resource|false —_writeCheck()
Check if instance can perform writes and print error if necessary
protected
_writeCheck() : bool
Return values
bool —True on writable