<?php

include '../src/openkm/OpenKM.php';

ini_set('display_errors', true);
error_reporting(E_ALL);

use openkm\OKMWebServicesFactory;
use openkm\OpenKM;

class ExampleAuth {

    const HOST = "http://localhost:8080/OpenKM/";
    const USER = "okmAdmin";
    const PASSWORD = "admin";

    private $ws;

    public function __construct() {
        $this->ws = OKMWebServicesFactory::build(self::HOST);
    }

    public function testLogin() {
        try {
            $this->ws->login();
            echo 'login';
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testGetGrantedRoles() {
        try {
            $this->ws->login(self::USER, self::PASSWORD);
            $grantedRoles = $this->ws->getGrantedRoles('4f75721e-6751-471f-a737-af318bce1ba1');
            foreach ($grantedRoles as $role) {
                var_dump($role);
            }
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testGetGrantedUsers() {
        try {
            $grantedUsers = $this->ws->getGrantedUsers('/okm:root/SDK4PHP');
            foreach ($grantedUsers as $user) {
                var_dump($user);
            }
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testGetRoles() {
        try {
            $roles = $this->ws->getRoles();
            foreach ($roles as $role) {
                var_dump($role);
            }
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testGetUsers() {
        try {
            $users = $this->ws->getUsers();
            foreach ($users as $user) {
                var_dump($user);
            }
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testGrantRole() {
        try {
            // Add ROLE_USER write grants at the node but not descendants
            $this->ws->grantRole("/okm:root/SDK4PHP", "ROLE_USER", \openkm\bean\Permission::ALL_GRANTS, false);

            // Add all ROLE_ADMIN grants to the node and descendants
            $this->ws->grantRole("/okm:root/SDK4PHP", "ROLE_ADMIN", \openkm\bean\Permission::ALL_GRANTS, true);

            echo 'grant Role';
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testGrantUser() {
        try {
            // Add sochoa write grants at the node but not descendants
            $this->ws->grantUser("/okm:root/SDK4PHP", "sochoa", \openkm\bean\Permission::ALL_GRANTS, false);

            // Add all okmAdmin grants at the node and descendants
            $this->ws->grantUser("/okm:root/SDK4PHP", "okmAdmin", \openkm\bean\Permission::ALL_GRANTS, true);

            echo 'grant User';
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testRevokeRole() {
        try {
            // Remove ROLE_USER write grants at the node but not descendants
            $this->ws->revokeRole("/okm:root/SDK4PHP", "ROLE_USER", \openkm\bean\Permission::ALL_GRANTS, false);

            // Remove all ROLE_ADMIN grants to the node and descendants
            $this->ws->revokeRole("/okm:root/SDK4PHP", "ROLE_ADMIN", \openkm\bean\Permission::ALL_GRANTS, true);

            echo 'revoke Role';
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testRevokeUser() {
        try {
            // Remove sochoa write grants at the node but not descendants
            $this->ws->revokeUser("/okm:root/SDK4PHP", "sochoa", \openkm\bean\Permission::ALL_GRANTS, false);

            // Remove all okmAdmin grants at the node and descendants
            $this->ws->revokeUser("/okm:root/SDK4PHP", "okmAdmin", \openkm\bean\Permission::ALL_GRANTS, true);
            echo 'revoke User';
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testGetRolesByUser() {
        try {
            $roles = $this->ws->getRolesByUser('okmAdmin');
            foreach ($roles as $role) {
                var_dump($role);
            }
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testGetUsersByRole() {
        try {
            $users = $this->ws->getUsersByRole('ROLE_ADMIN');
            foreach ($users as $user) {
                var_dump($user);
            }
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testGetMail() {
        try {
            var_dump($this->ws->getMail('okmAdmin'));
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testGetName() {
        try {
            var_dump($this->ws->getName('okmAdmin'));
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testCreateUser() {
        try {
            $this->ws->createUser('gnu.java.sergio', "sochoa", "sochoa@openkm.com", "Sergio Antonio Ochoa Martinez", true);
            echo 'create user';
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testDeleteUser() {
        try {
            $this->ws->deleteUser('gnu.java.sergio');
            echo 'delete user';
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testUpdateUser() {
        try {
            $this->ws->updateUser("gnu.java.sergio", "sochoa", "sochoa@openkm.com", "Sergio Ochoa", true);
            echo 'update user';
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testCreateRole() {
        try {
            $this->ws->createRole('ROLE_MANAGER', true);
            echo 'create role';
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testDeleteRole() {
        try {
            $this->ws->deleteRole('ROLE_MANAGER');
            echo 'delete role';
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testUpdateRole() {
        try {
            $this->ws->updateRole('ROLE_MANAGER', true);
            echo 'update role';
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testAssignRole() {
        try {
            $this->ws->assignRole('gnu.java.sergio', 'ROLE_MANAGER');
            echo 'assign role';
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testRemoveRole() {
        try {
            $this->ws->removeRole('gnu.java.sergio', 'ROLE_MANAGER');
            echo 'remove role';
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testChangeSecurity() {
        try {
            $changeSecurity = new \openkm\bean\ChangeSecurity();
            $changeSecurity->setNodeId("768748c5-04ab-4921-b0d4-7545d239ce5a");
            $changeSecurity->setRecursive(true);

            $grantedRoles = array();
            $grantedRole = new \openkm\bean\GrantedRole();
            $grantedRole->setRole('ROLE_MANAGER');
            $grantedRole->setPermissions(\openkm\bean\Permission::READ + \openkm\bean\Permission::WRITE);
            $grantedRoles[] = $grantedRole;

            $changeSecurity->setGrantedRoles($grantedRoles);

            $grantedUsers = array();
            $grantedUser = new \openkm\bean\GrantedUser();
            $grantedUser->setUser('gnu.java.sergio');
            $grantedUser->setPermissions(\openkm\bean\Permission::READ + \openkm\bean\Permission::WRITE);
            $grantedUsers[] = $grantedUser;
            $changeSecurity->setGrantedUsers($grantedUsers);

            $revokedRoles = array();
            $revokedRole = new \openkm\bean\RevokedRole();
            $revokedRole->setRole('ROLE_USER');
            $revokedRole->setPermissions(\openkm\bean\Permission::DELETE);
            $changeSecurity->setRevokedRoles($revokedRoles);

            $this->ws->changeSecurity($changeSecurity);
            echo 'change security';
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testGetProfiles() {
        try {
            $profiles = $this->ws->getProfiles(true);
            foreach ($profiles as $profile) {
                var_dump($profile);
            }
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testGetUserProfile() {
        try {
            var_dump($this->ws->getUserProfile('okmAdmin'));
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testSetUserProfile() {
        try {
            $profileId = 1;
            $this->ws->setUserProfile('sochoa', $profileId);
            echo 'set user profile';
        } catch (Exception $e) {
            var_dump($e);
        }
    }

}

$openkm = new OpenKM(); //autoload
$exampleAuth = new ExampleAuth();
$exampleAuth->testGetGrantedRoles();
?>