Skip to content
Grav 2.1 is out: every page speaks Markdown. Read the announcement →

Dynamically Adding a User to a Group

Started by Muut Archive 10 years ago · 1 replies · 407 views
10 years ago

I'd like to add a user to a group upon completing a purchase using the shopping cart plugin. That group would only be able to access certain pages, using the permission settings (think membership site). I cannot seem to be able to figure out how to automate this step so that I don't manually have to change the groups a user belongs to.

10 years ago

Basically you would need to take the user object, modify the group and then call save() on the object. However, you will also first need to set the file() on the object so it knows where to save it.

For example:

PHP
// Edit user object and save it
$user = $this->grav['user'];
$file = CompiledYamlFile::instance(Grav::instance()['locator']->findResource('user://accounts/' . $username . YAML_EXT, true, true));
$user->file($file);

// modify the user now...

$user->save();

Something like this....

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 2290 9 years ago
Archive · by Muut Archive, 9 years ago
2 1506 9 years ago
Archive · by Muut Archive, 9 years ago
2 4757 9 years ago
Archive · by Muut Archive, 9 years ago
1 3572 9 years ago
Archive · by Muut Archive, 9 years ago
3 1631 9 years ago