1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-15 23:52:33 +01:00
invoiceninja/app/Http/Controllers/TokenController.php

165 lines
3.8 KiB
PHP
Raw Normal View History

2017-01-30 20:40:43 +01:00
<?php
2015-03-16 22:45:25 +01:00
2017-01-30 20:40:43 +01:00
namespace App\Http\Controllers;
use App\Models\AccountToken;
use App\Services\TokenService;
2015-04-01 21:57:02 +02:00
use Auth;
use Input;
use Redirect;
2017-01-30 20:40:43 +01:00
use Session;
2015-04-02 15:12:12 +02:00
use URL;
2017-01-30 20:40:43 +01:00
use Validator;
use View;
2015-03-16 22:45:25 +01:00
/**
2017-01-30 20:40:43 +01:00
* Class TokenController.
*/
2015-03-16 22:45:25 +01:00
class TokenController extends BaseController
{
/**
* @var TokenService
*/
2015-11-05 23:37:04 +01:00
protected $tokenService;
/**
* TokenController constructor.
2017-01-30 20:40:43 +01:00
*
* @param TokenService $tokenService
*/
2015-11-05 23:37:04 +01:00
public function __construct(TokenService $tokenService)
{
2016-03-02 14:36:42 +01:00
//parent::__construct();
2015-11-05 23:37:04 +01:00
$this->tokenService = $tokenService;
}
/**
* @return \Illuminate\Http\RedirectResponse
*/
2015-10-21 13:11:08 +02:00
public function index()
{
return Redirect::to('settings/' . ACCOUNT_API_TOKENS);
}
/**
* @return \Illuminate\Http\JsonResponse
*/
2015-03-16 22:45:25 +01:00
public function getDatatable()
{
2016-05-08 20:50:35 +02:00
return $this->tokenService->getDatatable(Auth::user()->id);
2015-03-16 22:45:25 +01:00
}
/**
* @param $publicId
2017-01-30 20:40:43 +01:00
*
* @return \Illuminate\Contracts\View\View
*/
2015-03-16 22:45:25 +01:00
public function edit($publicId)
{
$token = AccountToken::where('account_id', '=', Auth::user()->account_id)
->where('public_id', '=', $publicId)->firstOrFail();
$data = [
'token' => $token,
'method' => 'PUT',
'url' => 'tokens/'.$publicId,
'title' => trans('texts.edit_token'),
];
return View::make('accounts.token', $data);
}
/**
* @param $publicId
2017-01-30 20:40:43 +01:00
*
* @return \Illuminate\Http\RedirectResponse
*/
2015-03-16 22:45:25 +01:00
public function update($publicId)
{
return $this->save($publicId);
}
/**
* @return \Illuminate\Http\RedirectResponse
*/
2015-03-16 22:45:25 +01:00
public function store()
{
return $this->save();
}
/**
* @return \Illuminate\Contracts\View\View
2015-03-16 22:45:25 +01:00
*/
public function create()
{
$data = [
'token' => null,
'method' => 'POST',
'url' => 'tokens',
'title' => trans('texts.add_token'),
];
return View::make('accounts.token', $data);
}
/**
* @return \Illuminate\Http\RedirectResponse
*/
2015-11-05 23:37:04 +01:00
public function bulk()
2015-03-16 22:45:25 +01:00
{
2015-11-05 23:37:04 +01:00
$action = Input::get('bulk_action');
$ids = Input::get('bulk_public_id');
$count = $this->tokenService->bulk($ids, $action);
2015-03-16 22:45:25 +01:00
2015-11-05 23:37:04 +01:00
Session::flash('message', trans('texts.archived_token'));
2015-03-16 22:45:25 +01:00
2015-10-14 16:15:39 +02:00
return Redirect::to('settings/' . ACCOUNT_API_TOKENS);
2015-03-16 22:45:25 +01:00
}
/**
* @param bool $tokenPublicId
2017-01-30 20:40:43 +01:00
*
* @return $this|\Illuminate\Http\RedirectResponse
2015-03-16 22:45:25 +01:00
*/
public function save($tokenPublicId = false)
{
if (Auth::user()->account->hasFeature(FEATURE_API)) {
2015-03-16 22:45:25 +01:00
$rules = [
'name' => 'required',
];
if ($tokenPublicId) {
$token = AccountToken::where('account_id', '=', Auth::user()->account_id)
->where('public_id', '=', $tokenPublicId)->firstOrFail();
}
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
return Redirect::to($tokenPublicId ? 'tokens/edit' : 'tokens/create')->withInput()->withErrors($validator);
}
if ($tokenPublicId) {
$token->name = trim(Input::get('name'));
} else {
$token = AccountToken::createNew();
$token->name = trim(Input::get('name'));
2017-04-02 19:46:01 +02:00
$token->token = strtolower(str_random(RANDOM_KEY_LENGTH));
2015-03-16 22:45:25 +01:00
}
$token->save();
if ($tokenPublicId) {
$message = trans('texts.updated_token');
} else {
$message = trans('texts.created_token');
}
Session::flash('message', $message);
}
2015-10-14 16:15:39 +02:00
return Redirect::to('settings/' . ACCOUNT_API_TOKENS);
2015-03-16 22:45:25 +01:00
}
}