User.create:after: Create new Page and change Status to "listed"

I´m using custom oAuth with the Kirby oAuth Plugin which creates a user on first login. I use the following hook to create a subpage with the username in my partners-folder as follows:

'user.create:after' => function( $user ) {

            if( $user->role() != 'partner' )
                return $user;

            $name_array            = array_filter( explode( ' ', $user->name() ) );
            $last_name_slug     = Str::slug( end( $name_array ) );
            $first_name_slug    = Str::slug( $name_array[0] );
            $partners                 = $this->site()->find('partners');
            # check for existing abbr of first name of user, if so lengthen by next following letter of first name
            $i = 1;
            do {
                $suffix = Str::substr( $first_name_slug, 0, $i );
                $exists = $partners->childrenAndDrafts()->find($last_name_slug.'-'.$suffix);
                $i++;
            } while( $exists );

            $args = [
                'template'  => 'partner',
                'slug'      => $last_name_slug.'-'.$suffix,
                'content'   => [
                    'title'     => $user->name(),
                    'users'     => [ $user->id() ]
                ]
            ];
            
            # Create Page and media folder
            $new_page = $partners->createChild( $args );
            Dir::make('content/partners/_drafts/'.$last_name_slug.'-'.$suffix.'/media');
            
            # Impersonate Kirby super user in case oAuth user does not have sufficient permissions
            $this->impersonate('kirby');
            $partner_page = $this->site()->find('partners')->childrenAndDrafts()->find($new_page->uri());
            $partner_page = $partner_page->changeStatus('listed','0');

        },

When I create the user manually as logged in admin, everything works fine but when using oAuth login, an error occurs: “Please use a valid Page Status”. As if status is not known to the system/user.

Am i missing something? Thanks for the help in advance.

Does the partner blueprint have allowed statuses set?

This is a part of my partner blueprint:

title: Partner

status:
    draft:
        label: Entwurf
        text: Die Seite ist im Entwurfsmodus und ist nur nach Anmeldung oder über den geheimen Link sichtbar
    listed:
        label: Veröffentlicht
        text: Die Seite ist öffentlich für alle

options:
    changeTitle: false
    changeStatus: false
    duplicate: false
    changeSlug: false
    delete: false

I also tried to allow the option of changeStatus to true, but does not solve the problem.

Could it be the issue, that I load the blueprints inside a plugin?

Your second argument should be an integer, not a string. If you use alphabetical sorting, set this in your blueprint and remove the second argument.

And changeStatus must be set to true if you want to allow changing the status.

1 Like

No.

You can set the changeStatus option based on user role.

Thanks that worked!

I will change the changeStatus to admin only. I guess when I use the impersonation, this should still work?

I think so, yes.

Just found out, that if I define status in a separate yml like this, it does not work anymore:

partner.yml

title: Partner

status: pages/status

status.yml:

draft:
    label: Entwurf
    text: Die Seite ist im Entwurfsmodus und ist nur nach Anmeldung oder über den geheimen Link sichtbar
listed:
    label: Veröffentlicht
    text: Die Seite ist öffentlich für alle

Should work, though. Although I wouldn’t put it into the pages. And you wrote you are doing this in a plugin. How did you register this status.yml?

It´s not a problem to put it directly in the blueprint.

I just noticed, I did not registered it in the plugin index.php, but it works in the panel, bot not on this issue.