Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Skip to content
ldap_account_creator.php 2.87 KiB
Newer Older
Akhil's avatar
Akhil committed
<?php
require 'vendor/autoload.php';
require_once('accounts/account_creator.php');
require_once('accounts/base_ecloud_account_creator.php');
require_once('accounts/ldap/user.php');
require_once('language.php');
require_once('helpers.php');

use LdapRecord\Connection;
use LdapRecord\Container;

class LDAPAccountCreator extends BaseEcloudAccountCreator
{
    private Connection $conn;

    private int $quotaInBytes = 1073741824;

    public function __construct(string $ecloudUrl)
    {
        parent::__construct($ecloudUrl);
        $quota = getenv('CLOUD_QUOTA_IN_BYTES');
        if (!$quota) {
            $this->quota = $this->quotaInBytes;
        } else {
            $this->quota = intval($quota);
        }
        $this->usernameIsEmail = false;
        $this->connectToLDAPServer();
    }

Nivesh Krishna's avatar
Nivesh Krishna committed
    protected function createAccount(object $userData)
Akhil's avatar
Akhil committed
    {
        $baseDn =  getenv('LDAP_USERS_BASE_DN');
Nivesh Krishna's avatar
Nivesh Krishna committed
        $userDn = "username=$userData->username," . $baseDn;
Akhil's avatar
Akhil committed
        $userClusterID = getenv('CLUSTER_ID');
        $answer = new \stdClass();
        try {
            $user =[
Nivesh Krishna's avatar
Nivesh Krishna committed
                'mailAddress' => $userData->email,
                'username' => $userData->username,
                'usernameWithoutDomain' => $userData->username,
                'userPassword' => $userData->password,
                'displayName' => $userData->name,
                'quota' => $userData->quota,
                'mailAlternate' => $userData->authmail,
                'recoveryMailAddress' => $userData->authmail,
Akhil's avatar
Akhil committed
                'active'=> 'TRUE',
                'mailActive' => 'TRUE',
                'userClusterID' => $userClusterID,
                'objectClass' => User::$objectClasses
            ];
            $userEntry = new User($user);
            $userEntry->setDn($userDn);
            $userEntry->save();
        } catch (Exception $e) {
            error_log('Error creating user ' . $e->getMessage());
            $answer->success= false;
            $answer->type = 'error_creating_account';
            return $answer;
        }
Nivesh Krishna's avatar
Nivesh Krishna committed
        $answer = $this->postCreationActions($userData, 'v2');
Akhil's avatar
Akhil committed
        return $answer;
    }

    private function getLDAPConfig() : array
    {
        $ldapHosts = getenv('LDAP_HOSTS');
        $ldapHosts = explode(",", $ldapHosts);
        $ldapPort = getenv('LDAP_PORT');
        $ldapAdminDn = getenv('LDAP_ADMIN_DN');
        $ldapAdminPassword = getenv('LDAP_ADMIN_PASSWORD');

        $baseDn = getenv('LDAP_USERS_BASE_DN');

        return [
          'hosts' => $ldapHosts,
          'port' => $ldapPort,
          'base_dn' =>   $baseDn,
          'username' => $ldapAdminDn,
          'password' => $ldapAdminPassword
        ];
    }

    private function connectToLDAPServer() : void
    {
        $config = $this->getLDAPConfig();

        $this->conn = new Connection(
            $config
        );
        Container::addConnection($this->conn);
    }
}