[solved] Plugin Api Procedure Handler


#1

Hello,

I developped a method to create a user by API request for my needs called by “createMyUser”. Currently this function is in “app/Api/Procedure/UserProcedure.php” but I want to do something cleaner and put it in a plugin (I already developped a plugin and this function could be linked to it).
Reading the documentation, I need to do something like :

$this->api->getProcedureHandler()->withCallback('my_method', function() {
    return 'foobar';
});

But I don’t know how to adapt to my purpose…
I think I should create a file located in my plugin directory “myPlugin/Procedure/MyUserProcedure.php”, create a “class UserProcedure extends BaseProcedure” (to access to User methods) and paste my function “createMyUser”.

public function createMyUser($username, $name = '', $email = '', $role = Role::APP_USER)
    {
        $values = array(
            'username' => $username,
            'name' => $name,
            'email' => $email,
            'role' => $role,
            'is_ldap_user' => 1,
        );

        //list($valid, ) = $this->userValidator->validateCreation($values); // For later...
        //return $valid ? $this->userModel->create($values) : false; // For later...
        return $this->userModel->create($values);
    }

Am I wrong ? Does anyone have a solution ?

Thank you !


#2

All plugins will have the same format and structure:
https://docs.kanboard.org/en/latest/plugins/registration.html

You’ll add your procedure to the Plugin.php file under the initialize function:
$this->api->getProcedureHandler()->withClassAndMethod('createMyUser', new UserProcedure($this->container), 'createMyUser');

For reference, I added some API procedures to Subtaskdate, you’ll basically do the same:


#3

Arg ! I looked at lot of plugin’s code but not yours !
Thank you, it works !