oauth and oauth2 apps manager

This commit is contained in:
Mario Vavti 2018-09-26 16:22:34 +02:00
parent 91502b4104
commit aab97adb23
8 changed files with 96 additions and 53 deletions

View File

@ -1,27 +1,33 @@
<?php
namespace Zotlabs\Module\Settings;
namespace Zotlabs\Module;
use App;
use Zotlabs\Lib\Apps;
use Zotlabs\Web\Controller;
class Oauth {
class Oauth extends Controller {
function post() {
if(! Apps::system_app_installed(local_channel(), 'OAuth'))
return;
if(x($_POST,'remove')){
check_form_security_token_redirectOnErr('/settings/oauth', 'settings_oauth');
check_form_security_token_redirectOnErr('/oauth', 'oauth');
$key = $_POST['remove'];
q("DELETE FROM tokens WHERE id='%s' AND uid=%d",
dbesc($key),
local_channel());
goaway(z_root()."/settings/oauth/");
goaway(z_root()."/oauth");
return;
}
if((argc() > 2) && (argv(2) === 'edit' || argv(2) === 'add') && x($_POST,'submit')) {
if((argc() > 1) && (argv(1) === 'edit' || argv(1) === 'add') && x($_POST,'submit')) {
check_form_security_token_redirectOnErr('/settings/oauth', 'settings_oauth');
check_form_security_token_redirectOnErr('oauth', 'oauth');
$name = ((x($_POST,'name')) ? escape_tags($_POST['name']) : '');
$key = ((x($_POST,'key')) ? escape_tags($_POST['key']) : '');
@ -73,17 +79,27 @@ class Oauth {
);
}
}
goaway(z_root()."/settings/oauth/");
goaway(z_root()."/oauth");
return;
}
}
function get() {
if((argc() > 2) && (argv(2) === 'add')) {
$tpl = get_markup_template("settings_oauth_edit.tpl");
if(! Apps::system_app_installed(local_channel(), 'OAuth Apps Manager')) {
//Do not display any associated widgets at this point
App::$pdl = '';
$o = '<b>OAuth App (Not Installed):</b><br>';
$o .= t('An OAuth apps manager');
return $o;
}
if((argc() > 1) && (argv(1) === 'add')) {
$tpl = get_markup_template("oauth_edit.tpl");
$o .= replace_macros($tpl, array(
'$form_security_token' => get_form_security_token("settings_oauth"),
'$form_security_token' => get_form_security_token("oauth"),
'$title' => t('Add application'),
'$submit' => t('Submit'),
'$cancel' => t('Cancel'),
@ -96,9 +112,9 @@ class Oauth {
return $o;
}
if((argc() > 3) && (argv(2) === 'edit')) {
if((argc() > 2) && (argv(1) === 'edit')) {
$r = q("SELECT * FROM clients WHERE client_id='%s' AND uid=%d",
dbesc(argv(3)),
dbesc(argv(2)),
local_channel());
if (!count($r)){
@ -107,9 +123,9 @@ class Oauth {
}
$app = $r[0];
$tpl = get_markup_template("settings_oauth_edit.tpl");
$tpl = get_markup_template("oauth_edit.tpl");
$o .= replace_macros($tpl, array(
'$form_security_token' => get_form_security_token("settings_oauth"),
'$form_security_token' => get_form_security_token("oauth"),
'$title' => t('Add application'),
'$submit' => t('Update'),
'$cancel' => t('Cancel'),
@ -122,13 +138,13 @@ class Oauth {
return $o;
}
if((argc() > 3) && (argv(2) === 'delete')) {
check_form_security_token_redirectOnErr('/settings/oauth', 'settings_oauth', 't');
if((argc() > 2) && (argv(1) === 'delete')) {
check_form_security_token_redirectOnErr('/oauth', 'oauth', 't');
$r = q("DELETE FROM clients WHERE client_id='%s' AND uid=%d",
dbesc(argv(3)),
dbesc(argv(2)),
local_channel());
goaway(z_root()."/settings/oauth/");
goaway(z_root()."/oauth");
return;
}
@ -141,11 +157,11 @@ class Oauth {
local_channel());
$tpl = get_markup_template("settings_oauth.tpl");
$tpl = get_markup_template("oauth.tpl");
$o .= replace_macros($tpl, array(
'$form_security_token' => get_form_security_token("settings_oauth"),
'$form_security_token' => get_form_security_token("oauth"),
'$baseurl' => z_root(),
'$title' => t('Connected Apps'),
'$title' => t('Connected OAuth Apps'),
'$add' => t('Add application'),
'$edit' => t('Edit'),
'$delete' => t('Delete'),

View File

@ -1,15 +1,21 @@
<?php
namespace Zotlabs\Module\Settings;
namespace Zotlabs\Module;
use App;
use Zotlabs\Lib\Apps;
use Zotlabs\Web\Controller;
class Oauth2 {
class Oauth2 extends Controller {
function post() {
if(! Apps::system_app_installed(local_channel(), 'OAuth2 Apps Manager'))
return;
if(x($_POST,'remove')){
check_form_security_token_redirectOnErr('/settings/oauth2', 'settings_oauth2');
check_form_security_token_redirectOnErr('oauth2', 'oauth2');
$name = ((x($_POST,'name')) ? escape_tags(trim($_POST['name'])) : '');
logger("REMOVE! ".$name." uid: ".local_channel());
$key = $_POST['remove'];
@ -25,13 +31,13 @@ class Oauth2 {
dbesc($name),
intval(local_channel())
);
goaway(z_root()."/settings/oauth2/");
goaway(z_root()."/oauth2");
return;
}
if((argc() > 2) && (argv(2) === 'edit' || argv(2) === 'add') && x($_POST,'submit')) {
if((argc() > 1) && (argv(1) === 'edit' || argv(1) === 'add') && x($_POST,'submit')) {
check_form_security_token_redirectOnErr('/settings/oauth2', 'settings_oauth2');
check_form_security_token_redirectOnErr('oauth2', 'oauth2');
$name = ((x($_POST,'name')) ? escape_tags(trim($_POST['name'])) : '');
$secret = ((x($_POST,'secret')) ? escape_tags(trim($_POST['secret'])) : '');
@ -80,17 +86,26 @@ class Oauth2 {
);
}
}
goaway(z_root()."/settings/oauth2/");
goaway(z_root()."/oauth2");
return;
}
}
function get() {
if((argc() > 2) && (argv(2) === 'add')) {
$tpl = get_markup_template("settings_oauth2_edit.tpl");
if(! Apps::system_app_installed(local_channel(), 'OAuth2 Apps Manager')) {
//Do not display any associated widgets at this point
App::$pdl = '';
$o = '<b>OAuth2 App (Not Installed):</b><br>';
$o .= t('An OAuth2 apps manager');
return $o;
}
if((argc() > 1) && (argv(1) === 'add')) {
$tpl = get_markup_template("oauth2_edit.tpl");
$o .= replace_macros($tpl, array(
'$form_security_token' => get_form_security_token("settings_oauth2"),
'$form_security_token' => get_form_security_token("oauth2"),
'$title' => t('Add OAuth2 application'),
'$submit' => t('Submit'),
'$cancel' => t('Cancel'),
@ -103,9 +118,9 @@ class Oauth2 {
return $o;
}
if((argc() > 3) && (argv(2) === 'edit')) {
if((argc() > 2) && (argv(1) === 'edit')) {
$r = q("SELECT * FROM oauth_clients WHERE client_id='%s' AND user_id= %d",
dbesc(argv(3)),
dbesc(argv(2)),
intval(local_channel())
);
@ -116,9 +131,9 @@ class Oauth2 {
$app = $r[0];
$tpl = get_markup_template("settings_oauth2_edit.tpl");
$tpl = get_markup_template("oauth2_edit.tpl");
$o .= replace_macros($tpl, array(
'$form_security_token' => get_form_security_token("settings_oauth2"),
'$form_security_token' => get_form_security_token("oauth2"),
'$title' => t('Add application'),
'$submit' => t('Update'),
'$cancel' => t('Cancel'),
@ -131,26 +146,26 @@ class Oauth2 {
return $o;
}
if((argc() > 3) && (argv(2) === 'delete')) {
check_form_security_token_redirectOnErr('/settings/oauth2', 'settings_oauth2', 't');
if((argc() > 2) && (argv(1) === 'delete')) {
check_form_security_token_redirectOnErr('oauth2', 'oauth2', 't');
$r = q("DELETE FROM oauth_clients WHERE client_id = '%s' AND user_id = %d",
dbesc(argv(3)),
dbesc(argv(2)),
intval(local_channel())
);
$r = q("DELETE FROM oauth_access_tokens WHERE client_id = '%s' AND user_id = %d",
dbesc(argv(3)),
dbesc(argv(2)),
intval(local_channel())
);
$r = q("DELETE FROM oauth_authorization_codes WHERE client_id = '%s' AND user_id = %d",
dbesc(argv(3)),
dbesc(argv(2)),
intval(local_channel())
);
$r = q("DELETE FROM oauth_refresh_tokens WHERE client_id = '%s' AND user_id = %d",
dbesc(argv(3)),
dbesc(argv(2)),
intval(local_channel())
);
goaway(z_root()."/settings/oauth2/");
goaway(z_root()."/oauth2");
return;
}
@ -164,9 +179,9 @@ class Oauth2 {
intval(local_channel())
);
$tpl = get_markup_template("settings_oauth2.tpl");
$tpl = get_markup_template("oauth2.tpl");
$o .= replace_macros($tpl, array(
'$form_security_token' => get_form_security_token("settings_oauth2"),
'$form_security_token' => get_form_security_token("oauth2"),
'$baseurl' => z_root(),
'$title' => t('Connected OAuth2 Apps'),
'$add' => t('Add application'),

6
app/oauth.apd Normal file
View File

@ -0,0 +1,6 @@
version: 1
url: $baseurl/oauth
requires: local_channel
name: OAuth Apps Manager
photo: icon:chevron-circle-up
categories: Access Control

6
app/oauth2.apd Normal file
View File

@ -0,0 +1,6 @@
version: 1
url: $baseurl/oauth2
requires: local_channel
name: OAuth2 Apps Manager
photo: icon:chevron-circle-up
categories: Access Control

View File

@ -4,13 +4,13 @@
</div>
<div class="section-content-tools-wrapper">
<form action="settings/oauth" method="post" autocomplete="off">
<form action="oauth" method="post" autocomplete="off">
<input type='hidden' name='form_security_token' value='{{$form_security_token}}'>
<div id="profile-edit-links">
<ul>
<li>
<a id="profile-edit-view-link" href="{{$baseurl}}/settings/oauth/add">{{$add}}</a>
<a id="profile-edit-view-link" href="{{$baseurl}}/oauth/add">{{$add}}</a>
</li>
</ul>
</div>
@ -25,8 +25,8 @@
{{/if}}
{{/if}}
{{if $app.my}}
<a href="{{$baseurl}}/settings/oauth/edit/{{$app.client_id}}" title="{{$edit}}"><i class="fa fa-pencil btn btn-outline-secondary"></i></a>
<a href="{{$baseurl}}/settings/oauth/delete/{{$app.client_id}}?t={{$form_security_token}}" title="{{$delete}}"><i class="fa fa-trash-o btn btn-outline-secondary"></i></a>
<a href="{{$baseurl}}/oauth/edit/{{$app.client_id}}" title="{{$edit}}"><i class="fa fa-pencil btn btn-outline-secondary"></i></a>
<a href="{{$baseurl}}/oauth/delete/{{$app.client_id}}?t={{$form_security_token}}" title="{{$delete}}"><i class="fa fa-trash-o btn btn-outline-secondary"></i></a>
{{/if}}
</div>
{{/foreach}}

View File

@ -8,13 +8,13 @@
<div id="profile-edit-links">
<ul>
<li>
<a id="profile-edit-view-link" href="{{$baseurl}}/settings/oauth2/add">{{$add}}</a>
<a id="profile-edit-view-link" href="{{$baseurl}}/oauth2/add">{{$add}}</a>
</li>
</ul>
</div>
{{foreach $apps as $app}}
<form action="settings/oauth2" method="post" autocomplete="off">
<form action="oauth2" method="post" autocomplete="off">
<input type='hidden' name='form_security_token' value='{{$form_security_token}}'>
<input type='hidden' name='name' value='{{$app.client_id}}'>
<div class='oauthapp'>
@ -25,8 +25,8 @@
{{/if}}
{{/if}}
{{if $app.my}}
<a href="{{$baseurl}}/settings/oauth2/edit/{{$app.client_id}}" title="{{$edit}}"><i class="fa fa-pencil btn btn-outline-secondary"></i></a>
<a href="{{$baseurl}}/settings/oauth2/delete/{{$app.client_id}}?t={{$form_security_token}}" title="{{$delete}}"><i class="fa fa-trash-o btn btn-outline-secondary"></i></a>
<a href="{{$baseurl}}/oauth2/edit/{{$app.client_id}}" title="{{$edit}}"><i class="fa fa-pencil btn btn-outline-secondary"></i></a>
<a href="{{$baseurl}}/oauth2/delete/{{$app.client_id}}?t={{$form_security_token}}" title="{{$delete}}"><i class="fa fa-trash-o btn btn-outline-secondary"></i></a>
{{/if}}
</div>
</form>