Skip to content

Commit

Permalink
log group CRUD
Browse files Browse the repository at this point in the history
  • Loading branch information
alexweissman committed Feb 28, 2017
1 parent 9495511 commit bcc1ac6
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 46 deletions.
4 changes: 4 additions & 0 deletions app/sprinkles/admin/locale/en_US/messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"CREATION_SUCCESSFUL" => "Successfully created group <strong>{{name}}</strong>",
"DELETE" => "Delete group",
"DELETE_CONFIRM" => "Are you sure you want to delete the group <strong>{{name}}</strong>?",
"DELETE_DEFAULT" => "You can't delete the group <strong>{{name}}</strong> because it is the default group for newly registered users.",
"DELETE_YES" => "Yes, delete group",
"DELETION_SUCCESSFUL" => "Successfully deleted group <strong>{{name}}</strong>",
"EDIT" => "Edit group",
Expand All @@ -57,6 +58,7 @@
"INFO_PAGE" => "Group information page for {{name}}",
"NAME" => "Group name",
"NAME_EXPLAIN" => "Please enter a name for the group",
"NOT_EMPTY" => "You can't do that because there are still users associated with the group <strong>{{name}}</strong>.",
"PAGE_DESCRIPTION" => "A listing of the groups for your site. Provides management tools for editing and deleting groups.",
"UPDATE" => "Details updated for group <strong>{{name}}</strong>"
],
Expand Down Expand Up @@ -87,9 +89,11 @@
"CREATION_SUCCESSFUL" => "Successfully created role <strong>{{name}}</strong>",
"DELETE" => "Delete role",
"DELETE_CONFIRM" => "Are you sure you want to delete the role <strong>{{name}}</strong>?",
"DELETE_DEFAULT" => "You can't delete the role <strong>{{name}}</strong> because it is a default role for newly registered users.",
"DELETE_YES" => "Yes, delete role",
"DELETION_SUCCESSFUL" => "Successfully deleted role <strong>{{name}}</strong>",
"EDIT" => "Edit role",
"HAS_USERS" => "You can't do that because there are still users who have the role <strong>{{name}}</strong>.",
"INFO_PAGE" => "Role information page for {{name}}",
"MANAGE" => "Manage Roles",
"NAME" => "Name",
Expand Down
40 changes: 29 additions & 11 deletions app/sprinkles/admin/src/Controller/GroupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public function delete($request, $response, $args)
// Need to use loose comparison for now, because some DBs return `id` as a string
if ($group->slug == $config['site.registration.user_defaults.group']) {
$e = new BadRequestException();
$e->addUserMessage('GROUP.DELETE_DEFAULT');
$e->addUserMessage('GROUP.DELETE_DEFAULT', $group->toArray());
throw $e;
}

Expand All @@ -173,14 +173,23 @@ public function delete($request, $response, $args)
$countGroupUsers = $classMapper->staticMethod('user', 'where', 'group_id', $group->id)->count();
if ($countGroupUsers > 0) {
$e = new BadRequestException();
$e->addUserMessage('GROUP.NOT_EMPTY');
$e->addUserMessage('GROUP.NOT_EMPTY', $group->toArray());
throw $e;
}

$groupName = $group->name;

$group->delete();
unset($group);
// Begin transaction - DB will be rolled back if an exception occurs
Capsule::transaction( function() use ($group, $groupName, $currentUser) {
$group->delete();
unset($group);

// Create activity record
$this->ci->userActivityLogger->info("User {$currentUser->user_name} deleted group {$groupName}.", [
'type' => 'group_delete',
'user_id' => $currentUser->id
]);
});

/** @var MessageStream $ms */
$ms = $this->ci->alerts;
Expand Down Expand Up @@ -257,7 +266,7 @@ public function getModalConfirmDelete($request, $response, $args)
$countGroupUsers = $classMapper->staticMethod('user', 'where', 'group_id', $group->id)->count();
if ($countGroupUsers > 0) {
$e = new BadRequestException();
$e->addUserMessage('GROUP.NOT_EMPTY');
$e->addUserMessage('GROUP.NOT_EMPTY', $group->toArray());
throw $e;
}

Expand Down Expand Up @@ -620,14 +629,23 @@ public function updateInfo($request, $response, $args)
return $response->withStatus(400);
}

// Update the group and generate success messages
foreach ($data as $name => $value) {
if ($value != $group->$name){
$group->$name = $value;
// Begin transaction - DB will be rolled back if an exception occurs
Capsule::transaction( function() use ($data, $group, $currentUser) {
// Update the group and generate success messages
foreach ($data as $name => $value) {
if ($value != $group->$name) {
$group->$name = $value;
}
}
}

$group->save();
$group->save();

// Create activity record
$this->ci->userActivityLogger->info("User {$currentUser->user_name} updated details for group {$group->name}.", [
'type' => 'group_update_info',
'user_id' => $currentUser->id
]);
});

$ms->addMessageTranslated('success', 'GROUP.UPDATE', [
'name' => $group->name
Expand Down
70 changes: 35 additions & 35 deletions app/sprinkles/admin/src/Controller/RoleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,39 +121,6 @@ public function create($request, $response, $args)
return $response->withStatus(200);
}

/**
* Returns a list of Roles
*
* Generates a list of roles, optionally paginated, sorted and/or filtered.
* This page requires authentication.
* Request type: GET
*/
public function getList($request, $response, $args)
{
// GET parameters
$params = $request->getQueryParams();

/** @var UserFrosting\Sprinkle\Account\Authorize\AuthorizationManager */
$authorizer = $this->ci->authorizer;

/** @var UserFrosting\Sprinkle\Account\Model\User $currentUser */
$currentUser = $this->ci->currentUser;

// Access-controlled page
if (!$authorizer->checkAccess($currentUser, 'uri_roles')) {
throw new ForbiddenException();
}

/** @var UserFrosting\Sprinkle\Core\Util\ClassMapper $classMapper */
$classMapper = $this->ci->classMapper;

$sprunje = $classMapper->createInstance('role_sprunje', $classMapper, $params);

// Be careful how you consume this data - it has not been escaped and contains untrusted user-supplied content.
// For example, if you plan to insert it into an HTML DOM, you must escape it on the client side (or use client-side templating).
return $sprunje->toResponse($response);
}

/**
* Processes the request to delete an existing role.
*
Expand Down Expand Up @@ -233,6 +200,39 @@ public function delete($request, $response, $args)
return $response->withStatus(200);
}

/**
* Returns a list of Roles
*
* Generates a list of roles, optionally paginated, sorted and/or filtered.
* This page requires authentication.
* Request type: GET
*/
public function getList($request, $response, $args)
{
// GET parameters
$params = $request->getQueryParams();

/** @var UserFrosting\Sprinkle\Account\Authorize\AuthorizationManager */
$authorizer = $this->ci->authorizer;

/** @var UserFrosting\Sprinkle\Account\Model\User $currentUser */
$currentUser = $this->ci->currentUser;

// Access-controlled page
if (!$authorizer->checkAccess($currentUser, 'uri_roles')) {
throw new ForbiddenException();
}

/** @var UserFrosting\Sprinkle\Core\Util\ClassMapper $classMapper */
$classMapper = $this->ci->classMapper;

$sprunje = $classMapper->createInstance('role_sprunje', $classMapper, $params);

// Be careful how you consume this data - it has not been escaped and contains untrusted user-supplied content.
// For example, if you plan to insert it into an HTML DOM, you must escape it on the client side (or use client-side templating).
return $sprunje->toResponse($response);
}

public function getModalConfirmDelete($request, $response, $args)
{
// GET parameters
Expand Down Expand Up @@ -267,15 +267,15 @@ public function getModalConfirmDelete($request, $response, $args)
// Need to use loose comparison for now, because some DBs return `id` as a string
if (in_array($role->slug, $defaultRoleSlugs)) {
$e = new BadRequestException();
$e->addUserMessage('ROLE.DELETE_DEFAULT');
$e->addUserMessage('ROLE.DELETE_DEFAULT', $role->toArray());
throw $e;
}

// Check if there are any users associated with this role
$countUsers = $role->users()->count();
if ($countUsers > 0) {
$e = new BadRequestException();
$e->addUserMessage('ROLE.HAS_USERS');
$e->addUserMessage('ROLE.HAS_USERS', $role->toArray());
throw $e;
}

Expand Down

0 comments on commit bcc1ac6

Please sign in to comment.