Skip to content

Commit

Permalink
Plugin Auth: Factorisation…
Browse files Browse the repository at this point in the history
  • Loading branch information
Seza committed Jun 23, 2012
1 parent 49ebf75 commit bd02232
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 154 deletions.
64 changes: 16 additions & 48 deletions app/plugins/auth/admin/account.php
Expand Up @@ -5,58 +5,26 @@
use pixelpost\core\Template,
pixelpost\plugins\api\Plugin as Api;

// who is connected
$user_online = Plugin::get_entity_name();
$flag_success = false;
$flag_reconnect = false;

// is form posted ?
if ($event->request->is_post())
{
// retrieve posted data in $post
$post = filter_var_array($event->request->get_post(), array(
'name' => array('filter' => FILTER_SANITIZE_STRING),
'email' => array('filter' => FILTER_VALIDATE_EMAIL),
'password' => array('filter' => FILTER_SANITIZE_STRING),
));

// delete not provided and bad value
if (!$post['name']) unset($post['name']);
if (!$post['email']) unset($post['email']);
if (!$post['password']) unset($post['password']);

// remove name if not changed
if ($post['name'] && $post['name'] == $user_online) unset($post['name']);

// make the update
Api::call_api_method('auth.user.set', $post + array('user' => $user_online));

// template response
$flag_success = 'Updated.';

if (isset($post['name']) || isset($post['password']))
{
$flag_reconnect .= ' You need to reconnect on next page.';
}

if (isset($post['name'])) $user_online = $post['name'];
}
// create the user form
$form = new classes\UserForm(Plugin::get_entity_name(), true);

// retrieve user data
$user = Api::call_api_method('auth.user.get', array('user' => $user_online));
// check if form is posted and process data
$form->check($event->request);

// create API request
$user = array('user' => $form->user_id);

// retrieve user data
$user += Api::call_api_method('auth.user.get', $user);
$user += array('gravatar' => md5(strtolower($user['email'])));

// retrieve user entities
$entities = Api::call_api_method('auth.entity.list', array());
// retrieve user entities and grants
$entities = current(Api::call_api_method('auth.entity.list'));
$grants = current(Api::call_api_method('auth.grant.list', $user));

// retrieve user grant
$grants = Api::call_api_method('auth.grant.list', array('user' => $user_online));
// create the form template
$form_tpl = $form->render($user);

Template::create()
->assign('flag_success', $flag_success)
->assign('flag_reconnect', $flag_reconnect)
->assign('user', $user)
->assign('entities', $entities['list'])
->assign('grants', $grants['list'])
->publish('auth/tpl/account.tpl');
->assign(compact('form_tpl', 'user', 'entities', 'grants'))
->publish('auth/tpl/account.tpl');
78 changes: 78 additions & 0 deletions app/plugins/auth/classes/UserForm.php
@@ -0,0 +1,78 @@
<?php

namespace pixelpost\plugins\auth\classes;

use pixelpost\core\Request,
pixelpost\core\Template,
pixelpost\plugins\api\Plugin as Api;

class UserForm
{
public $flag_reconnect = false;
public $flag_success = false;
public $is_online = false;
public $user_id = null;

public function __construct($user_id, $is_online = false)
{
$this->user_id = $user_id;
$this->is_online = $is_online;
}

/**
* Check if the form is posted and process posted data
*
* @param pixelpost\core\Request $request
* @return bool TRUE if a form is posted else false
*/
public function check(Request $request)
{
if (!$request->is_post()) return false;

// retrieve posted data in $p
$p = filter_var_array($request->get_post(), array(
'name' => array('filter' => FILTER_SANITIZE_STRING),
'email' => array('filter' => FILTER_VALIDATE_EMAIL),
'password' => array('filter' => FILTER_SANITIZE_STRING),
));

// delete not provided and bad value
if (!$p['name']) unset($p['name']);
if (!$p['email']) unset($p['email']);
if (!$p['password']) unset($p['password']);

// remove name if not changed (for error in api next call: same name)
if (isset($p['name']) && $p['name'] === $this->user_id)
{
unset($p['name']);
}

// make the update
Api::call_api_method('auth.user.set', $p + array('user' => $this->user_id));

// update some flag
$this->flag_success = true;
$this->flag_reconnect = ($this->is_online and isset($p['name']) || isset($p['password']));

// update the user id if needed
if (isset($p['name']))
{
$this->user_id = $p['name'];
}

return true;
}

/**
* Create a Html5 form
*
* @param array|object $user the user data (user, name, email)
* @return string The HTML5 form
*/
public function render($user)
{
return Template::create()
->assign(array('user' => $user, 'form' => $this))
->render('auth/tpl/_user-form.tpl');
}
}
1 change: 0 additions & 1 deletion app/plugins/auth/public/account.js
Expand Up @@ -130,7 +130,6 @@ $(document).ready(function() {
{
var e = new account_entity(this);
e.bind();
e.dom.show();
});

$('#key_add').click(function()
Expand Down
41 changes: 41 additions & 0 deletions app/plugins/auth/tpl/_entity.tpl
@@ -0,0 +1,41 @@
<div class="entity well well-small">
<p>
<strong>{{ entity.name }}</strong>
<span class="hide">&mdash;</span>
<span class="pull-right">
<a class="btn btn-info btn-mini" data-toggle="collapse" data-parent="#entities" href="#{{ entity.entity }}">
<i class="icon-list icon-white"></i> info
</a>
<span class="hide">&mdash;</span>
<a class="btn btn-danger btn-mini">
<i class="icon-remove icon-white"></i> delete
</a>
</span>
</p>
<form id="{{ entity.entity }}" class="collapse form-inline">
<table class="table table-condensed">
<tr>
<th>Public key</th>
<td>{{ entity.public_key }}</td>
</tr>
<tr>
<th>Private key</th>
<td>{{ entity.private_key }}</td>
</tr>
<tr>
<th>Grants</th>
<td>
{% for g in grants %}
<label class="checkbox inline">
<input type="checkbox" value="{{ g.grant }}"> {{ g.name }}
</label>
{% endfor %}
</td>
</tr>
</table>
<p class="input-append">
<input type="text" placeholder="rename that public key"><button
class="btn"><i class="icon-pencil"></i>update</button>
</p>
</form>
</div>
4 changes: 4 additions & 0 deletions app/plugins/auth/tpl/_entity_form.tpl
@@ -0,0 +1,4 @@
<p class="well form-inline">
<input id="key_name" placeholder="Where will this key be used?">
<button class="btn" id="key_add">Create</button>
</p>
16 changes: 16 additions & 0 deletions app/plugins/auth/tpl/_entity_modal.tpl
@@ -0,0 +1,16 @@
<div class="modal fade" id="modal_del_entity">
<div class="modal-header">
<a class="close" data-dismiss="modal">×</a>
<h3>Are you sure?</h3>
</div>
<div class="modal-body">
<p>
Public key <strong></strong> will be deleted. Software using this
key will no longer be able to access to your pixelpost information.
</p>
</div>
<div class="modal-footer">
<a href="#" class="btn" data-dismiss="modal">Cancel</a>
<a href="#" class="btn btn-danger btn-primary">Delete</a>
</div>
</div>
39 changes: 39 additions & 0 deletions app/plugins/auth/tpl/_user-form.tpl
@@ -0,0 +1,39 @@
<form id="form-account" method="post" accept-charset="utf-8" class="form-horizontal">
<fieldset>
<legend>Update account settings</legend>
{% if form.flag_success %}
<p class="alert alert-success fade in">
<a class="close" data-dismiss="alert">&times;</a>
Updated !
</p>
{% endif %}
{% if form.flag_reconnect %}
<p class="alert alert-warning fade in">
<a class="close" data-dismiss="alert">&times;</a>
You need to reconnect on next page.
</p>
{% endif %}
<div class="control-group">
<label class="control-label" for="name">Username:</label>
<div class="controls">
<input id="name" name="name" value="{{ user.name }}" placeholder="username" required>
</div>
</div>
<div class="control-group">
<label class="control-label" for="email">Email:</label>
<div class="controls">
<input type="email" id="email" name="email" value="{{ user.email }}" placeholder="user@example.com" required>
</div>
</div>
<div class="control-group">
<label class="control-label" for="password">Password:</label>
<div class="controls">
<input type="password" id="password" name="password" placeholder="keep it empty will not change it">
</div>
</div>
<div class="form-actions">
<button class="btn btn-primary" type="submit">Update</button>
<button class="btn" type="reset">Reset</button>
</div>
</fieldset>
</form>

0 comments on commit bd02232

Please sign in to comment.