Skip to content

Commit

Permalink
Integrated Public API
Browse files Browse the repository at this point in the history
  • Loading branch information
ConsoleTVs committed Sep 10, 2016
1 parent 4d7dfa4 commit e0e0f1e
Show file tree
Hide file tree
Showing 15 changed files with 324 additions and 41 deletions.
83 changes: 83 additions & 0 deletions app/Http/Controllers/Laralum/APIController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace App\Http\Controllers\Laralum;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Schema;
use DB;
use Laralum;

class APIController extends Controller
{
/**
* Shows the API List
*/
public function index()
{
return view('laralum/API/index');
}

/**
* Shows the API json
*
* @param string $table
* @param string $accessor
* @param string $data
*/
public function show($table, $accessor = null, $data = null)
{
$api = Laralum::apiData();

if(!Schema::hasTable($table) or !array_key_exists($table, $api)){
abort(404);
}

if(!$api[$table]['enabled']){
return ['error' => trans('api.api_disabled')];
}

$columns = Schema::getColumnListing($table);

if($accessor){

if($accessor == 'latest') {
$rows = DB::table($table)->orderBy('id', 'desc')->limit(1)->get();
} elseif($accessor == 'latests'){
if(!$data){
$data = 5;
}
$rows = DB::table($table)->orderBy('id', 'desc')->limit($data)->get();
} else {
if(!Schema::hasColumn($table, $accessor)){
abort(404);
}
$rows = DB::table($table)->where($accessor, $data)->get();
}

} else {
$rows = DB::table($table)->get();
}

$final_columns = [];

foreach($columns as $column){
if(in_array($column, $api[$table]['show'])){
array_push($final_columns, $column);
}
}

$final_rows = [];
foreach($rows as $row){
$final_row = [];
foreach($final_columns as $column){
$final_row[$column] = $row->$column;
}
array_push($final_rows, $final_row);
}

return $final_rows;
}
}
58 changes: 58 additions & 0 deletions app/Http/Controllers/Laralum/Data/API.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

$api = [
'users' => [
'show' => [
'id', 'name', 'email', 'active', 'banned', 'country_code', 'created_at', 'updated_at'
],
'enabled' => true,
],
'users_settings' => [
'show' => [
'default_role', 'location', 'register_enabled', 'default_active', 'welcome_email'
],
'enabled' => true,
],
'roles' => [
'show' => [
'id', 'name', 'color', 'assignable', 'allow_editing', 'created_at', 'updated_at'
],
'enabled' => true,
],
'permissions' => [
'show' => [
'id', 'slug', 'color', 'assignable', 'created_at', 'updated_at'
],
'enabled' => true,
],
'blogs' => [
'show' => [
'id', 'name', 'views_location', 'user_id', 'created_at', 'updated_at'
],
'enabled' => true,
],
'posts' => [
'show' => [
'id', 'image', 'title', 'description', 'body', 'user_id', 'blog_id', 'edited_by', 'created_at', 'updated_at'
],
'enabled' => true,
],
'post_comments' => [
'show' => [
'id', 'user_id', 'post_id', 'name', 'email', 'content', 'created_at', 'updated_at'
],
'enabled' => true,
],
'documents' => [
'show' => [
'id', 'user_id', 'name', 'slug', 'disabled', 'download_timer', 'downloads', 'created_at', 'updated_at'
],
'enabled' => true,
],
'settings' => [
'show' => [
'laralum_version', 'website_title', 'logo'
],
'enabled' => true,
],
];
8 changes: 8 additions & 0 deletions app/Http/Controllers/Laralum/Laralum.php
Original file line number Diff line number Diff line change
Expand Up @@ -1053,5 +1053,13 @@ public static function dataPath()
return app_path() . '/Http/Controllers/Laralum/Data';
}

public static function apiData()
{
require('Data/API.php');
if($api){
return $api;
}
}


}
30 changes: 1 addition & 29 deletions app/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,10 @@ class User extends Authenticatable
* @var array
*/
protected $hidden = [
'password', 'remember_token',
'password', 'remember_token', 'activation_key',
];

/**
* setNameAttribute
*
* Mutator to capitalize the name
*
* @param mixed $value
Expand All @@ -43,8 +41,6 @@ public function setNameAttribute($value){
}

/**
* roles
*
* Returns all the roles from the user
*
*/
Expand All @@ -54,8 +50,6 @@ public function roles()
}

/**
* isAdmin
*
* Returns true if the user has access to laralum
*
*/
Expand All @@ -65,8 +59,6 @@ public function isAdmin()
}

/**
* hasPermission
*
* Returns true if the user has the permission slug
*
* @param string $slug
Expand All @@ -84,8 +76,6 @@ public function hasPermission($slug)
}

/**
* hasRole
*
* Returns true if the user has the role
*
* @param string $name
Expand All @@ -101,8 +91,6 @@ public function hasRole($name)
}

/**
* blogs
*
* Returns all the blogs owned by the user
*
*/
Expand All @@ -112,8 +100,6 @@ public function blogs()
}

/**
* has_blog
*
* Returns true if the user has blog access
*
* @param number $id
Expand All @@ -131,8 +117,6 @@ public function has_blog($id)
}

/**
* owns_blog
*
* Returns true if the user owns the blog
*
* @param number $id
Expand All @@ -147,8 +131,6 @@ public function owns_blog($id)
}

/**
* posts
*
* Returns all the posts from the user
*
*/
Expand All @@ -158,8 +140,6 @@ public function posts()
}

/**
* owns_post
*
* Returns true if the users owns the post
*
* @param number $id
Expand All @@ -174,8 +154,6 @@ public function owns_post($id)
}

/**
* avatar
*
* Returns the user avatar from Gavatar
*
* @param number $size
Expand All @@ -189,8 +167,6 @@ public function avatar($size = null){
}

/**
* documents
*
* Returns all the documents from the user
*
*/
Expand All @@ -200,8 +176,6 @@ public function documents()
}

/**
* sendWelcomeEmail
*
* Sends the welcome email notification to the user
*
*/
Expand All @@ -211,8 +185,6 @@ public function sendWelcomeEmail()
}

/**
* sendActivationEmail
*
* Sends the activation email notification to the user
*
*/
Expand Down
2 changes: 1 addition & 1 deletion database/migrations/2016_06_11_160205_create_settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function up()
});

$settings = new Settings;
$settings->laralum_version = "2.0.6";
$settings->laralum_version = "2.1";
$settings->website_title = env('APP_NAME', 'My Application');
$settings->logo = ''; //Default logo will load
$settings->button_color = "blue";
Expand Down
16 changes: 16 additions & 0 deletions resources/lang/ca/api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

return [

/*
|--------------------------------------------------------------------------
| API Language Lines
|--------------------------------------------------------------------------
|
| All the API files
|
*/

'api_disabled' => "Aquesta API està desactivada",

];
16 changes: 13 additions & 3 deletions resources/lang/ca/laralum.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,9 @@

'security_description_title' => "Què és aquesta pàgina?",
'security_description' => "Aquesta pàgina és una pàgina de confirmació per evitar
coses que és possible que no vulguis fer, o simplement
per fer-li saber que aquesta acció no es pot desfer i
és per això que requereix quelcom més que un sol clic
coses que és possible que no vulguis fer, o simplement
per fer-li saber que aquesta acció no es pot desfer i
és per això que requereix quelcom més que un sol clic
per a ser executada.",

/*
Expand Down Expand Up @@ -543,4 +543,14 @@
'activation_not_valid' => "El codi d'activació no és vàlid",
'activation_account' => "Activació de compte",
'activation_key' => "Clau d'activació",

/*
|--------------------------------------------------------------------------
| VERSION 2.1.X
|--------------------------------------------------------------------------
*/
'laralum_API' => "API de Laralum",
'API_subtitle' => "API pública de Laralum (Tindrà codis d'API aviat)",
'API_url' => "URL de l'API",
'API_show' => "Columnes que es mostraran",
];
16 changes: 16 additions & 0 deletions resources/lang/en/api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

return [

/*
|--------------------------------------------------------------------------
| API Language Lines
|--------------------------------------------------------------------------
|
| All the API files
|
*/

'api_disabled' => "The current API is disabled",

];
10 changes: 10 additions & 0 deletions resources/lang/en/laralum.php
Original file line number Diff line number Diff line change
Expand Up @@ -543,4 +543,14 @@
'activation_not_valid' => "The activation code is not valid",
'activation_account' => "Account Activation",
'activation_key' => "Activation Key",

/*
|--------------------------------------------------------------------------
| VERSION 2.1.X
|--------------------------------------------------------------------------
*/
'laralum_API' => "Laralum API",
'API_subtitle' => "Laralum Public API (Will soon have API keys)",
'API_url' => "API URL",
'API_show' => "Columns that will be shown",
];
16 changes: 16 additions & 0 deletions resources/lang/es/api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

return [

/*
|--------------------------------------------------------------------------
| API Language Lines
|--------------------------------------------------------------------------
|
| All the API files
|
*/

'api_disabled' => "Esta API está desactivada",

];
Loading

0 comments on commit e0e0f1e

Please sign in to comment.