Skip to content

Commit

Permalink
Nonces and authorisation for grants.
Browse files Browse the repository at this point in the history
  • Loading branch information
samwilson committed Feb 15, 2016
1 parent b4ce697 commit 3b73b64
Show file tree
Hide file tree
Showing 4 changed files with 220 additions and 66 deletions.
124 changes: 120 additions & 4 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

159 changes: 98 additions & 61 deletions src/Controllers/GrantsController.php
Original file line number Diff line number Diff line change
@@ -1,61 +1,98 @@
<?php

namespace WordPress\Tabulate\Controllers;

class GrantsController extends ControllerBase {

/** @var array|string */
private $table_names;

/** @var \WordPress\Tabulate\Template */
private $template;

public function __construct($wpdb) {
parent::__construct($wpdb);
$db = new \WordPress\Tabulate\DB\Database( $this->wpdb );
$this->table_names = $db->get_table_names();
$this->template = new \WordPress\Tabulate\Template( 'grants.html' );
}

public function index() {
$this->template->tables = $this->table_names;
$grants = new \WordPress\Tabulate\DB\Grants();
$this->template->roles = $grants->get_roles();
$this->template->grants = $grants->get();
$this->template->capabilities = $grants->get_capabilities();
$this->template->form_action = $this->get_url( 'save' );
return $this->template->render();
}

public function save() {
$grants = new \WordPress\Tabulate\DB\Grants();

// Validate the POSTed grants.
$new_grants = array();
foreach ($_POST as $table => $table_grants) {
if ( in_array( $table, $this->table_names ) ) {
$new_grants[$table] = array();
foreach ($table_grants as $capability => $roles) {
if ( in_array( $capability, $grants->get_capabilities() ) ) {
$new_grants[$table][$capability] = array_keys($roles);
}
}
}
}

// Save the grants and return to the granting table.
$grants->set( $new_grants );
$this->template->add_notice( 'updated', 'Grants saved.' );
wp_redirect( $this->get_url( 'index' ) );
exit;
}

/**
* Get the URL of the grants' admin page.
* @param string $action Either 'save' or 'index'.
* @return string
*/
public function get_url( $action ) {
return admin_url( 'admin.php?page=tabulate&controller=grants&action=' . $action );
}
}
<?php
/**
* This file contains only a single class.
*
* @package Tabulate
*/

namespace WordPress\Tabulate\Controllers;

/**
* The GrantsController enables viewing and saving of grants.
*/
class GrantsController extends ControllerBase {

/**
* The list of tables.
*
* @var string[]
*/
private $table_names;

/**
* The Template.
*
* @var \WordPress\Tabulate\Template
*/
private $template;

/**
* Prevent non-admin users from doing anything here (i.e. redirect and exit
* instead). Otherwise, setup the list of tables and the template.
*
* @param wpdb $wpdb The global wpdb object.
*/
public function __construct( $wpdb ) {
parent::__construct( $wpdb );
if ( ! current_user_can( 'promote_users' ) ) {
$url = admin_url( 'admin.php?page=tabulate' );
wp_redirect( $url );
exit;
}
$db = new \WordPress\Tabulate\DB\Database( $this->wpdb );
$this->table_names = $db->get_table_names();
$this->template = new \WordPress\Tabulate\Template( 'grants.html' );
}

/**
* Get the HTML table of grants.
*
* @return string
*/
public function index() {
$this->template->tables = $this->table_names;
$grants = new \WordPress\Tabulate\DB\Grants();
$this->template->roles = $grants->get_roles();
$this->template->grants = $grants->get();
$this->template->capabilities = $grants->get_capabilities();
$this->template->form_action = $this->get_url( 'save' );
return $this->template->render();
}

/**
* Save the POSTed grants array.
*/
public function save() {
check_admin_referer( 'tabulate-grants' );
$grants = new \WordPress\Tabulate\DB\Grants();

// Validate the POSTed grants.
$new_grants = array();
foreach ( $_POST as $table => $table_grants ) {
if ( in_array( $table, $this->table_names, true ) ) {
$new_grants[ $table ] = array();
foreach ( $table_grants as $capability => $roles ) {
if ( in_array( $capability, $grants->get_capabilities(), true ) ) {
$new_grants[ $table ][ $capability ] = array_keys( $roles );
}
}
}
}

// Save the grants and return to the granting table.
$grants->set( $new_grants );
$this->template->add_notice( 'updated', 'Grants saved.' );
wp_redirect( $this->get_url( 'index' ) );
exit;
}

/**
* Get the URL of the grants' admin page.
*
* @param string $action Either 'save' or 'index'.
* @return string
*/
public function get_url( $action ) {
return admin_url( 'admin.php?page=tabulate&controller=grants&action=' . $action );
}
}
2 changes: 1 addition & 1 deletion src/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public function render() {
$twig = new \Twig_Environment( $loader );

// Add some useful functions to Twig.
$funcs = array( 'admin_url', '__', '_e' );
$funcs = array( 'admin_url', '__', '_e', 'wp_nonce_field' );
foreach ( $funcs as $f ) {
$twig->addFunction( $f, new \Twig_SimpleFunction( $f, $f ) );
}
Expand Down
1 change: 1 addition & 0 deletions templates/grants.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ <h2>Grants</h2>
<p>Here you can grant access to the database to members of any Roles.</p>

<form action="{{form_action}}" method="post" class="tabulate-grants">
{{wp_nonce_field('tabulate-grants')|raw}}
<table class="widefat">
<thead>
<tr>
Expand Down

0 comments on commit 3b73b64

Please sign in to comment.