Skip to content

Commit

Permalink
precurser for admin settings and SSO
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Blanquera committed Jan 15, 2019
1 parent f6eaac2 commit d507c98
Show file tree
Hide file tree
Showing 9 changed files with 893 additions and 26 deletions.
7 changes: 5 additions & 2 deletions .cradle.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
* This file is part of a Custom Package.
*/
require_once __DIR__ . '/package/events.php';
require_once __DIR__ . '/src/events.php';
require_once __DIR__ . '/src/controller.php';
require_once __DIR__ . '/package/helpers.php';
require_once __DIR__ . '/src/events.php';
require_once __DIR__ . '/src/controller/auth.php';
require_once __DIR__ . '/src/controller/admin.php';


//bootstrap
$this->preprocess(include(__DIR__ . '/src/bootstrap/attempts.php'));
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"satooshi/php-coveralls": "2.0.0"
},
"require": {
"cradlephp/cradle-profile": "0.*"
"cradlephp/cradle-profile": "~2.2.0",
"cradlephp/cradle-captcha": "~2.2.0"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 1 addition & 1 deletion package/schema/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
'fields' => [
[
'disable' => '1',
'label' => 'Slug',
'label' => 'Email',
'name' => 'slug',
'field' => [
'type' => 'text',
Expand Down
37 changes: 32 additions & 5 deletions src/bootstrap/attempts.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* A helper to manage login attempts
*/
$package->addMethod('getAttempts', function (RequestInterface $request) {
$attempts = $request->getSession('login_attempts');
$attempts = $request->getSession('auth_attempts');

if (!is_array($attempts)) {
$attempts = [];
Expand All @@ -29,7 +29,7 @@
* A helper to manage login attempts
*/
$package->addMethod('clearAttempts', function (RequestInterface $request) {
$request->removeSession('login_attempts');
$request->removeSession('auth_attempts');
return $this;
});

Expand All @@ -39,26 +39,53 @@
$package->addMethod('addAttempt', function (RequestInterface $request) {
$attempts = $this->getAttempts($request);
array_unshift($attempts, time());
$request->setSession('login_attempts', $attempts);
$request->setSession('auth_attempts', $attempts);
return $attempts;
});

/**
* Returns how long someone should wait before logging in again
*/
$package->addMethod('waitFor', function (RequestInterface $request) {
$config = $this->config();
$attempts = $this->getAttempts($request);

//allow a few attempts
if (count($attempts) < 5) {
if (count($attempts) < $config['lockout']) {
return 0;
}

$wait = ($attempts[0] + (60 * 5)) - time();
$wait = ($attempts[0] + (60 * $config['wait'])) - time();

if ($wait < 0) {
$wait = 0;
}

return $wait;
});

/**
* Returns how long someone should wait before logging in again
*/
$package->addMethod('config', function () {
$config = cradle('global')->config('auth', 'submission');

if (!is_array($config)) {
$config = [];
}

if (!isset($config['captcha'])) {
$config['captcha'] = 2;
}

if (!isset($config['lockout'])) {
$config['lockout'] = 5;
}

if (!isset($config['wait'])) {
$config['wait'] = 5;
}

return $config;
});
};
189 changes: 189 additions & 0 deletions src/controller/admin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
<?php //-->
/**
* This file is part of a Custom Project.
* (c) 2016-2018 Acme Products Inc.
*
* Copyright and license information can be found at LICENSE.txt
* distributed with this package.
*/

use Cradle\Package\System\Schema;

/**
* Render the Admin Settings Page
*
* @param Request $request
* @param Response $response
*/
$this->get('/admin/system/model/auth/settings', function ($request, $response) {
//----------------------------//
// 1. Prepare Data
//get schema data
$schema = Schema::i('auth');

//Prepare body
$data = ['item' => $request->getPost()];

if (empty($data['item'])) {
$global = $this->package('global');
$item['service']['facebook'] = $global->config('services', 'facebook-main');
$item['service']['twitter'] = $global->config('services', 'twitter-main');
$item['service']['linkedin'] = $global->config('services', 'linkedin-main');
$item['service']['google'] = $global->config('services', 'google-main');
$item['service']['captcha'] = $global->config('services', 'captcha-main');
$item['submission']['attempt'] = $global->config('auth', 'submission');

foreach ($item['service'] as $service => $setting) {
if (!is_array($setting)) {
continue;
}

foreach ($setting as $key => $value) {
if (strpos($value, '<') === 0) {
$setting[$key] = null;
}
}

$item['service'][$service] = $setting;
}

$data['item'] = $item;
}

//also pass the schema to the template
$data['schema'] = $schema->getAll();

//add CSRF
$this->trigger('csrf-load', $request, $response);
$data['csrf'] = $response->getResults('csrf');

if ($response->isError()) {
$response->setFlash($response->getMessage(), 'error');
$data['errors'] = $response->getValidation();
}

//----------------------------//
// 2. Render Template
//Render body
$class = 'admin-system-model-auth-settings page-admin';
$title = $this->package('global')->translate('Authentication Settings');

$template = dirname(__DIR__) . '/template';
if (is_dir($response->getPage('template_root'))) {
$template = $response->getPage('template_root');
}

$partials = dirname(__DIR__) . '/template';
if (is_dir($response->getPage('partials_root'))) {
$partials = $response->getPage('partials_root');
}

$body = $this
->package('cradlephp/cradle-system')
->template(
'admin/settings',
$data,
[],
$template,
$partials
);

//Set Content
$response
->setPage('title', $title)
->setPage('class', $class)
->setContent($body);

//if we only want the body
if ($request->getStage('render') === 'body') {
return;
}

//Render blank page
$this->trigger('admin-render-page', $request, $response);
});

/**
* Process the Admin Settings Page
*
* @param Request $request
* @param Response $response
*/
$this->post('/admin/system/model/auth/settings', function ($request, $response) {
//----------------------------//
// 1. Prepare Data
$config = $this->package('global')->config('services');
$services = $request->getStage('service');
$submission = $request->getStage('submission');

//----------------------------//
// 2. Validate Data
//----------------------------//
// 3. Process Request
if (is_array($services)) {
$invalid = ['sql', 'elastic', 'redis', 'rabbitmq', 's3', 'mail'];
foreach ($services as $name => $service) {
if (in_array($name, $invalid)) {
continue;
}

foreach ($service as $key => $value) {
if ($key !== 'active' && !trim($value)) {
$service[$key] = sprintf(
'<%s %s>',
strtoupper($name),
strtoupper($key)
);
}
}

$config[$name . '-main'] = $service;
}

$this->package('global')->config('services', $config);
}

$submission = [];
$settings = ['captcha' => 2, 'lockout' => 4, 'wait' => 5];
foreach ($settings as $setting => $default) {
$submission[$setting] = $request->getStage(
'submission',
'attempt',
$setting
);

if (!is_numeric($submission[$setting])) {
$submission[$setting] = $default;
}
}

$this->package('global')->config('auth', 'submission', $submission);

//----------------------------//
// 4. Interpret Results
//record logs
$this->log(
'updated auth settings',
$request,
$response,
'settings'
);

//redirect
$redirect = '/admin/system/model/auth/search';

//if there is a specified redirect
if ($request->getStage('redirect_uri')) {
//set the redirect
$redirect = $request->getStage('redirect_uri');
}

//if we dont want to redirect
if ($redirect === 'false') {
return;
}

//add a flash
$this->package('global')->flash('Auth settings were updated', 'success');
$this->package('global')->redirect($redirect);
});
Loading

0 comments on commit d507c98

Please sign in to comment.