You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
169 lines
6.4 KiB
169 lines
6.4 KiB
const Unit = require('libflitter/Unit')
|
|
|
|
/*
|
|
* The Miscellaneous Unit
|
|
* -------------------------------------------------------------
|
|
* This is a Unit file where you should make any modifications that
|
|
* your application may need such as custom Express add-ons. Changes
|
|
* here are loaded after the core Flitter units, but before the other
|
|
* units (secondary, custom, error, and App), so they are available to
|
|
* other units in the stack.
|
|
*/
|
|
class MiscUnit extends Unit {
|
|
|
|
/*
|
|
* Initializes the unit class.
|
|
* This is called OUTSIDE of a Flitter context,
|
|
* so no other contexts are available. Modifications here
|
|
* take place before any part of Flitter is loaded or available.
|
|
*/
|
|
constructor(){
|
|
super()
|
|
|
|
|
|
}
|
|
|
|
/*
|
|
* Initialize the actual Unit.
|
|
* This is where most of the changes will go.
|
|
* This is provided an instance of the Express app
|
|
* so any custom changes can be made. The core Flitter
|
|
* contexts are available here, so things like config(),
|
|
* model(), etc. work.
|
|
*/
|
|
async go(app, context){
|
|
|
|
String.prototype.capitalize = function(){
|
|
return this.charAt(0).toUpperCase() + this.slice(1)
|
|
}
|
|
|
|
// do stuff here
|
|
global.devbug = {
|
|
version: '0.6.0',
|
|
get_inline: function(type, project){
|
|
let code = this.code[type];
|
|
if (!code) return false
|
|
|
|
code = code.replace(/{{ Project API Key }}/g, project.uuid).replace(/{{ Server URL }}/g, _flitter.config('server.url'))
|
|
return code
|
|
},
|
|
code: {
|
|
node: `require("devbugjs")
|
|
dbsetup({
|
|
\tserver: "{{ Server URL }}", // DevBug Server URL
|
|
\tproject: "{{ Project API Key }}", // Project API Key
|
|
})`,
|
|
api: `// Send a multipart/form-data POST request to:
|
|
// http://#{_flitter.config('server.url')}/api/v1/out/<project api key>
|
|
|
|
// The form should have a single field with the name "data".
|
|
// This field should contain a valid JSON string with the following structure:
|
|
{
|
|
"brief": "Some preview text to be displayed. Whatever you want.",
|
|
"data": {
|
|
// the output data (key-pairs) goes here:
|
|
"some name": "some data",
|
|
},
|
|
}`,
|
|
php: `<?php
|
|
// ===========================================================
|
|
// DEVBUG INLINE DEBUGGING HELPER - FOR USE WITH DEVBUG SERVER
|
|
// TODO: REMOVE BEFORE COMMITTING
|
|
$dev_outs = [];
|
|
function out($key, $what, $group = null){
|
|
global $dev_outs;
|
|
if ( $group ){
|
|
if ( !array_key_exists($group, $dev_outs) ){
|
|
$dev_outs[$group] = [ $key => $what ];
|
|
}
|
|
else {
|
|
$dev_outs[$group][$key] = $what;
|
|
}
|
|
}
|
|
else {
|
|
$dev_outs[$key] = $what;
|
|
}
|
|
}
|
|
function breakpoint($continue = false, $name = null){
|
|
global $dev_outs;
|
|
$devbug = "{{ Server URL }}";
|
|
$project_api_key = "{{ Project API Key }}";
|
|
$bt = debug_backtrace();
|
|
$caller = array_shift($bt);
|
|
|
|
// Send to devbug server
|
|
$ch = curl_init();
|
|
$url = $devbug.'api/v1/out/'.$project_api_key;
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|
curl_setopt($ch, CURLOPT_POST, 1);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, [
|
|
'data' => json_encode([
|
|
'brief' => ($name ? $name : 'Breakpoint').': '.$caller['file'].': '.$caller['line'],
|
|
'data' => $dev_outs,
|
|
])
|
|
]);
|
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
|
$odata = curl_exec($ch);
|
|
|
|
if ( !$continue ) { exit(); }
|
|
}
|
|
// ===========================================================`,
|
|
},
|
|
permission: {
|
|
project: {
|
|
async edit(project, target_user){
|
|
if ( project.user_id === target_user.uuid ) return true
|
|
else if ( project.edit_user_ids.includes(target_user.uuid) ) return true
|
|
else return false
|
|
},
|
|
sync_edit(project, target_user){
|
|
if ( project.user_id === target_user.uuid ) return true
|
|
else if ( project.edit_user_ids.includes(target_user.uuid) ) return true
|
|
else return false
|
|
},
|
|
async view(project, target_user){
|
|
if ( project.user_id === target_user.uuid ) return true
|
|
else if ( project.shared_user_ids.includes(target_user.uuid) ) return true
|
|
else if ( project.edit_user_ids.includes(target_user.uuid) ) return true
|
|
else return false
|
|
},
|
|
async owns(project, target_user){
|
|
return (project.user_id === target_user.uuid)
|
|
}
|
|
},
|
|
snippet: {
|
|
async edit(snippet, target_user){
|
|
const project = await _flitter.model('v1:Project').findById(snippet.project_id)
|
|
if ( snippet.user_id === target_user.uuid ) return true
|
|
else if ( snippet.edit_user_ids.includes(target_user.uuid) ) return true
|
|
else if ( await devbug.permission.project.edit(project, target_user) ) return true
|
|
else return false
|
|
},
|
|
async view(snippet, target_user){
|
|
const project = await _flitter.model('v1:Project').findById(snippet.project_id)
|
|
if ( this.edit(snippet, target_user) ) return true
|
|
else if ( snippet.shared_user_ids.includes(target_user.uuid) ) return true
|
|
else if ( await devbug.permission.project.view(project, target_user) ) return true
|
|
else return false
|
|
},
|
|
async owns(snippet, target_user){
|
|
const project = await _flitter.model('v1:Project').findById(snippet.project_id)
|
|
if ( snippet.user_id === target_user.uuid ) return true
|
|
else if ( await devbug.permission.project.owns(project, target_user) ) return true
|
|
else return false
|
|
}
|
|
}
|
|
},
|
|
}
|
|
}
|
|
|
|
name(){
|
|
return "misc"
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = exports = MiscUnit
|