Skip to content
Grav 2.0 is officially stable. Read the announcement →
Archive

Dynamically Adding a User to a Group

Started by Muut Archive 10 years ago · 1 replies · 341 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 1362 9 years ago
Archive · by Muut Archive, 9 years ago
2 939 9 years ago
Archive · by Muut Archive, 9 years ago
2 4069 9 years ago
Archive · by Muut Archive, 9 years ago
1 2959 9 years ago
Archive · by Muut Archive, 9 years ago
3 1124 9 years ago