started token API

This commit is contained in:
2021-03-23 00:41:12 -04:00
parent b97970c8de
commit 623e52e135
5 changed files with 506 additions and 17 deletions
+14
View File
@@ -243,6 +243,20 @@ app.group = (function(app){
return {list, remove}
})(app)
app.token = (function(app){
function list(name, callack){
if($.isFunction(name)){
callack = name;
name = ''
}
app.api.get('token/'+name+'?detail=true', function(error, data){
callack(error, data);
});
}
return {list}
})(app)
app.util = (function(app){
function getUrlParameter(name) {
+245
View File
@@ -0,0 +1,245 @@
(function($, Mustache){
'use strict';
if (!$.scope) {
$.scope = {};
}
var make = function( element ){
//construct array
function makeArray( input ){
var result = [];
Object.defineProperty( result, "__repeatId", {
value: repeatId,
writable: true,
enumerable: false,
configurable: true
} );
result.splice = function(){
//splice does all the heavy lifting by interacting with the DOM elements.
var index;
//if a string is submitted as the index, try to match it to index number
if( typeof arguments[0] === 'string' ){
index = this.indexOf( arguments[0] );//set where to start
if ( index === -1 ) {
return [];
}
}else{
index = arguments[0]; //set where to start
}
var howMany = arguments[1]; //sets the amount of fields to remove
var args = Array.prototype.slice.call( arguments ); // coverts arguments into array
var toAdd = args.slice(2); // only keeps fields to add to array
// if the starting point is higher then the total index count, start at the end
if( index > this.length ) {
index = this.length;
}
// if the starting point is negative, start form the end of the array, minus the start point
if( index < 0 ) {
index = this.length - Math.abs( index );
}
// if there are things to add, figure out the how many new indexes we need
if( !howMany && howMany !== 0 ) {
howMany = this.length - index;
}
//not sure why i put this here... but it does matter!
if( howMany > this.length - index ) {
howMany = this.length - index;
}
//figure out how many positions we need to shift the current elements
var shift = toAdd.length - howMany;
// figure out how big the new array will be
// var newLength = this.length + shift;
//removes fields from array based on howMany needs to be removed
for( var i = 0; i < howMany; i++ ) {
this.__take.apply( $( '.jq-repeat-'+ this.__repeatId +'[jq-repeat-index="'+ ( i + index ) +'"]' ) );
}
//re-factor element index's
$( '.jq-repeat-'+ this.__repeatId+'[jq-repeat-index!="holder"]' ).each(function(){
var thisIndex = Number( $( this ).attr( 'jq-repeat-index' ) );
if( thisIndex >= index){
$( this ).attr( 'jq-repeat-index', thisIndex+shift );
}
});
//if there are fields to add to the array, add them
if( toAdd.length > 0 ){
//$.each( toAdd, function( key, value ){
for(var I = 0; I < toAdd.length; I++){
//figure out new elements index
var key = I + index;
//get the proper template
var template = $( document.getElementById( this.__repeatId + 'Template' ).outerHTML );
// apply values to template
var render = Mustache.render( template.html(), toAdd[I] );
//set call name and index keys to DOM element
render = $( render ).addClass( 'jq-repeat-'+ this.__repeatId ).attr( 'jq-repeat-index', key );
//if add new elements in proper stop, or after the place holder.
if( key === 0 ){
$( '.jq-repeat-'+ this.__repeatId +'[jq-repeat-index="holder"]' ).after( render );
}else{
$( '.jq-repeat-'+ this.__repeatId +'[jq-repeat-index="' + ( key -1 ) + '"]' ).after( render );
}
//animate element
this.__put.apply(render, [toAdd[I]]);
}
}
//set and return new array
return Array.prototype.splice.apply( this, arguments );
};
result.push = function(){
//add one or more objects to the array
//set the index value, if none is set make it zero
var index = this.length || 0;
//loop each passed object and pass it to slice
for (var i = 0 ; i < arguments.length; ++i) {
this.splice( ( index + i ), 0, arguments[i] );
}
//return new array length
return this.length;
};
result.pop = function(){
//remove and return array element
return this.splice( -1, 1 )[0];
};
result.reverse = function() {
var temp = this.splice( 0 );
Array.prototype.reverse.apply( temp );
for( var i = 0; i < temp.length; i++ ){
this.push( temp[i] );
}
return this;
};
result.shift = function() {
return this.splice( 0, 1 )[0];
};
result.loop = function(){
var temp = this[0];
this.splice( 0,1 );
this.push( temp );
return temp;
};
result.loopUp = function(){
var temp = this[this.length-1];
this.splice( -1, 1 );
this.splice( 0, 0, temp );
return temp;
};
result.indexOf = function( key, value ){
if( typeof value !== 'string' ){
value = arguments[0];
key = this.__index;
}
for ( var index = 0; index < this.length; ++index ) {
if( this[index][key] === value ){
return index;
}
}
return -1;
};
result.update = function( key, value, update ){
//set variables using sting for index
if( typeof value !== 'string' ){
update = arguments[1];
value = arguments[0];
key = this.__index;
}
var index = this.indexOf( key, value );
if(index === -1) {
return [];
}
var object = $.extend( true, {}, this[index], update );
return this.splice( index, 1, object )[0];
};
result.__put = function(){
this.show();
};
result.__take = function(){
this.remove();
};
if(!input) {
return result;
}
$.each( input, function( key, value ){
var type = typeof value;
if( type === 'object' ){
result.push( value );
}else if( type === 'string' ){
Object.defineProperty( result, "__index", {
value: value,
writable: true,
enumerable: false,
configurable: true
} );
} else if ( type === 'function'){
Object.defineProperty( result, value.name, {
value: value,
writable: true,
enumerable: false,
configurable: true
} );
}
} );
return result;
}
var $this = $( element );
var repeatId = $this.attr( 'jq-repeat' );
var tempId = repeatId + 'Template';
var templateId = $( '#' + tempId ).html();
$this.removeAttr( 'jq-repeat' );
$this.wrap( '<script type="x-tmpl-mustache" id="' + tempId + '" class="jq-repeat-' + repeatId + ' " jq-repeat-index="holder"><\/script>' );
Mustache.parse(templateId); // optional, speeds up future uses
$.scope[repeatId] = makeArray($.scope[repeatId]);
//return {};
};
$( document ).ready( function(){
$( '[jq-repeat]' ).each(function(key, value){
make(value);
});
$(document).on('DOMNodeInserted', function(e) {
//console.log(e.target.is('[jq-repeat]'));
if ( $(e.target).is('[jq-repeat]') ){
make( e.target );
}else{
var t = $(e.target).find('[jq-repeat]');
t.each(function(key, value){
make(value);
});
}
});
} );
})(jQuery, Mustache);
+13 -12
View File
@@ -4,24 +4,31 @@ var express = require('express');
var router = express.Router();
const moment = require('moment');
const {InviteToken, PasswordResetToken} = require('./../models/token');
const conf = require('../conf/conf.js');
/* GET home page. */
router.get('/', async function(req, res, next) {
res.render('home', { title: 'Express' });
res.render('home', { title: 'Express', name: conf.name });
});
router.get('/login', function(req, res, next) {
res.render('login', {redirect: req.query.redirect, name: conf.name });
});
/* GET home page. */
router.get('/users', function(req, res, next) {
res.render('users', { title: 'Express' });
res.render('users', { title: 'Express', name: conf.name });
});
router.get('/users/:uid', function(req, res, next) {
res.render('home', { title: 'Express' });
res.render('home', { title: 'Express', name: conf.name });
});
router.get('/groups', function(req, res, next) {
res.render('groups', { title: 'Express' });
res.render('groups', { title: 'Express', name: conf.name });
});
router.get('/token', function(req, res, next) {
res.render('token', { title: 'Express', name: conf.name });
});
@@ -66,10 +73,4 @@ router.get('/login/invite/:token', async function(req, res, next){
}
});
/* GET home page. */
router.get('/login', function(req, res, next) {
res.render('login', {redirect: req.query.redirect});
});
module.exports = router;
+17 -5
View File
@@ -2,12 +2,24 @@
const router = require('express').Router();
const {AuthToken} = require('../models/auth');
const {Token, InviteToken} = require('../models/token');
var tokens = require('../models/token');
// const {Token, InviteToken, PasswordResetToken} = require('../models/token');
const tokens = {
auth: AuthToken,
invite: InviteToken
}
delete tokens.Token;
// const tokens = {
// auth: AuthToken,
// invite: InviteToken,
// password: PasswordResetToken,
// }
router.get('/', async function(req, res, next){
try{
return res.json({results: Object.keys(tokens)})
}catch(error){
throw(error);
}
});
router.get('/:name', async function(req, res, next){
try{
+217
View File
@@ -0,0 +1,217 @@
<%- include('top') %>
<script id="rowTemplate" type="text/html">
<div id="group-card-{{cn}}" class="card shadow">
<div class="card-header">
<h5>
<i class="fad fa-users-class"></i>
Group: {{ cn }}
</h5>
<ul class="nav nav-tabs card-header-tabs" id="myTab" role="tablist">
<li class="nav-item">
<a class="nav-link active" id="group-members-tab-{{cn}}" data-toggle="tab" href="#group-memmbers-{{cn}}" role="tab" aria-controls="member" aria-selected="true">
<i class="fad fa-users"></i>
Members
</a>
</li>
<li class="nav-item">
<a class="nav-link" id="group-admins-tab-{{cn}}" data-toggle="tab" href="#group-admins-{{cn}}" role="tab" aria-controls="admin" aria-selected="false">
<i class="fad fa-users-crown"></i>
Owners
</a>
</li>
<li class="nav-item float-right">
</li>
</ul>
</div>
<div class="card-header actionMessage" style="display:none"></div>
<div class="card-body">
<p>
{{ description }}
</p>
<div class="tab-content" id="myTabContent">
<div class="tab-pane fade show active" id="group-memmbers-{{cn}}" role="tabpanel" aria-labelledby="member-tab">
<p>
<ul class="list-group">
{{ #member }}
<li id="group-card-{{cn}}-{{uid}}" class="list-group-item shadow">
<i class="fad fa-user"></i> {{ uid }}
<button type="button" action="group/{{groupCN}}/{{uid}}" method="delete" onclick="formAJAX(this)" evalAJAX="addedUser(data.message, '{{groupCN}}', '{{uid}}', $form)" class="btn btn-sm btn-danger float-right">
<i class="fad fa-user-slash"></i>
</button>
</li>
{{ /member }}
</ul>
</p>
<div class="dropdown">
<button class="btn btn-secondary dropdown-toggle" type="button" id="group_add_member" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<i class="fad fa-user-plus"></i>
</button>
<div class="dropdown-menu shadow-lg" aria-labelledby="group_add_member">
{{ #toAdd }}{{#.}}
<a class="dropdown-item" action="group/{{groupCN}}/{{uid}}" method="put" onclick="formAJAX(this)" evalAJAX="addedUser(data.message, '{{groupCN}}', '{{uid}}', $form);">
<i class="fad fa-user"></i> {{uid}}
</a>
{{/.}}{{ /toAdd }}
</div>
</div>
</div>
<div class="tab-pane fade" id="group-admins-{{cn}}" role="tabpanel" aria-labelledby="admin-tab">
<p>
<ul class="list-group">
{{ #owner }}
<li class="list-group-item shadow">
<i class="fad fa-user"></i> {{ uid }}
<button type="button" action="group/owner/{{groupCN}}/{{uid}}" method="delete" onclick="formAJAX(this)" evalAJAX="addedUser(data.message, '{{groupCN}}', '{{uid}}', $form)" class="btn btn-sm btn-danger float-right">
<i class="fad fa-user-slash"></i>
</button>
</li>
{{ /owner }}
</ul>
</p>
<div class="dropdown float-left">
<button class="btn btn-secondary dropdown-toggle" type="button" id="group_add_admin" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<i class="fad fa-user-crown"></i>
</button>
<div class="dropdown-menu shadow-lg" aria-labelledby="group_add_admin">
{{ #toAddOwner }}{{#.}}
<a class="dropdown-item" action="group/owner/{{groupCN}}/{{uid}}" method="put" onclick="formAJAX(this)" evalAJAX="addedUser(data.message, '{{groupCN}}', '{{uid}}', $form)">
<i class="fad fa-user"></i> {{uid}}
</a>
{{/.}}{{ /toAddOwner }}
</div>
</div>
</div>
</div>
</div>
<div class="card-footer">
<div class="float-left">
Created: {{createTimestamp}}<br />
Last Mortified: {{modifyTimestamp}}
</div>
<div class="float-right">
<button type="button" onclick="" class="btn btn-warning btn-lg shadow">
<i class="fad fa-edit"></i>
</button>
<button type="button" onclick="app.group.remove({cn: '{{cn}}'}, function(){tableAJAX('Group {{cn}} deleted.')})" class="btn btn-danger btn-lg">
<i class="fad fa-trash"></i>
</button>
</div>
</div>
</div>
<br/>
</script>
<script type="text/javascript">
var userlist;
function getUserList(callback){
app.user.list(function(error, data){
userlist = data.results;
callback()
});
}
function addedUser(message, group, user, $form){
tableAJAX(null, function(){
app.util.actionMessage(message, $("#group-card-"+group), 'success');
$('a[href="#'+$form.closest('.tab-pane').attr('id')+'"]').tab('show');
setTimeout(function(group){
$("body,html").animate({
scrollTop: $("#group-card-" + group).offset().top
}, 0);
}, 400, group);
});
}
function tableAJAX(actionMessage, callback){
var rowTemplate = $('#rowTemplate').html();
var $target = $('#tableAJAX');
$target.html('').hide();
app.util.actionMessage('Refreshing user list...', $target);
app.group.list(function(error, data){
var out = ''
$.each(data.results, function(key, value) {
value.toAdd = userlist.map(function(user){
if(!value.member.includes(user.dn)) return user;
})
value.toAddOwner = userlist.map(function(user){
if(!value.owner.includes(user.dn)) return user;
})
value.member = value.member.map(function(user){
return {
dn: user,
uid: user.match(/cn=[a-zA-Z0-9\_\-\@\.]+/)[0].replace('cn=', '')
}
})
value.owner = value.owner.map(function(user){
return {
dn: user,
uid: user.match(/cn=[a-zA-Z0-9\_\-\@\.]+/)[0].replace('cn=', '')
}
})
value.createTimestamp = moment(value.createTimestamp, "YYYYMMDDHHmmssZ").fromNow();
value.modifyTimestamp = moment(value.modifyTimestamp, "YYYYMMDDHHmmssZ").fromNow();
value.groupCN = value.cn;
out += Mustache.render(rowTemplate, value);
});
$target.html(out);
$target.fadeIn('slow', ($.isFunction(callback) ? callback: function(){})());
app.util.actionMessage(actionMessage || '', $target, 'info');
});
}
$(document).ready(function(){
getUserList(tableAJAX);
});
</script>
<div class="row" style="display:none">
<div class="col-md-4">
<div class="card shadow-lg">
<div class="card-header">
<i class="fas fa-layer-plus"></i>
Add new group
</div>
<div class="card-header actionMessage" style="display:none"></div>
<div class="card-body">
<form action="group/" method="post" onsubmit="formAJAX(this)" evalAJAX="tableAJAX('')">
<div class="form-group">
<label class="control-label">Name</label>
<input type="text" class="form-control shadow" name="name" placeholder="app_gitea_admin" validate=":3" />
</div>
<div class="form-group">
<label class="control-label">Description</label>
<textarea class="form-control shadow" name="description" placeholder="Admin group for gitea app" validate=":3"></textarea>
</div>
<button type="submit" class="btn btn-outline-dark">Add</button>
</form>
</div>
</div>
</div>
<div class="col-md-8" id="tableAJAX">
</div>
</div>
<%- include('bottom') %>