Jump to content

Set template access rules from api


fbg13
 Share

Recommended Posts

I'm trying to create a site with multiple blogs, run by different users.
Each user only being able to see and manage to their blog.
For each blog I create a role, a template, an user and a page.

I'm creating everything through the api and only one thing doesn't work.
When I login as one of these users I can't see the page (in the pages tree in the admin dashboard) set up as their blog root.

But if I go and edit role assigned to the user and just click save (no other changes)
then the page appears after a refresh and I can edit it and add child pages.

Here's the code

Spoiler
function create(string $name, string $blogName) {

	$role_ = wire()->roles->get("{$name}_blog_editor");
	if (!$role_->id) {
		$role_ = wire()->roles->add("{$name}_blog_editor");
		$permission_ = wire()->permissions->get("page-edit");
		$role_->addPermission($permission_);
	}

	$user_ = wire()->users->get("{$name}");
	if (!$user_->id) {
		$user_ = wire()->users->add("{$name}");
		$user_->pass = "q";
		$user_->addRole($role_);
		$user_->save();
	}

	$homeTemplate = wire()->templates->get("home");
	$blogPostTemplate = wire()->templates->get("blog_post");
	if (!$blogPostTemplate->id) {
		$blogPostTemplate = wire()->templates->add("blog_post");
		$blogPostTemplate->noChildren = 1;
		$blogPostTemplate->save();
	}

	$template_ = wire()->templates->get("{$name}_blog");
	if (!$template_->id) {
		$template_ = wire()->templates->add("{$name}_blog");

		// give access to the role
		$template_->useRoles = true;
		// only the newly created role (and the admin) has access
		$template_->editRoles   = [$role_->id]; // array of roles that can edit pages with this template
		$template_->createRoles = [$role_->id]; // ... can create pages ...
		$template_->addRoles    = [$role_->id]; // ... can add child pages ...
		$template_->addRole($role_);
		$role_->save();
		$template_->save();

		// limit the child and parent templates
		$template_->childTemplates([$blogPostTemplate]);
		$template_->parentTemplates([$homeTemplate]);

		// add the title field
		$template_->fieldgroup->add("title");
		$template_->fieldgroup->save();
		$template_->save();

		// set as allowed parent template for the "blog_post" template
		$blogPostTemplate->parentTemplates($blogPostTemplate->parentTemplates()->append($template_));
		$blogPostTemplate->save();
	}

	$page_ = wire()->pages->get("template={$name}_blog, name={$name}");
	if (!$page_->id) {
		$page_ = wire()->pages->add($template_, "/", ["title" => "{$blogName}", "name" => "{$name}"]);
		$page_->save();
	}

}

 

What am I doing wrong?

Link to comment
Share on other sites

Figured it out... seems like I have to get the role again and turn of output formatting

	...

	$page_ = wire()->pages->get("template={$name}_blog, name={$name}");
	if (!$page_->id) {
		$page_ = wire()->pages->add($template_, "/", ["title" => "{$blogName}", "name" => "{$name}"]);
		$page_->save();
	}

	$role_ = wire()->roles->get("{$name}_blog_editor");
    if ($role_->id) {
        $role_->of(false);
        $role_->save();
    }
}

 

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...