1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/app/Http/Controllers/TokenController.php

135 lines
3.2 KiB
PHP
Raw Normal View History

2015-03-17 02:30:56 +01:00
<?php namespace App\Http\Controllers;
2015-03-16 22:45:25 +01:00
2015-04-01 21:57:02 +02:00
use Auth;
use Session;
use DB;
use Validator;
use Input;
use View;
use Redirect;
2015-04-02 15:12:12 +02:00
use Datatable;
use URL;
2015-04-01 21:57:02 +02:00
use App\Models\AccountToken;
2015-11-05 23:37:04 +01:00
use App\Services\TokenService;
use App\Ninja\Repositories\AccountRepository;
2015-03-16 22:45:25 +01:00
class TokenController extends BaseController
{
2015-11-05 23:37:04 +01:00
protected $tokenService;
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;
}
2015-10-21 13:11:08 +02:00
public function index()
{
return Redirect::to('settings/' . ACCOUNT_API_TOKENS);
}
2015-03-16 22:45:25 +01:00
public function getDatatable()
{
2015-11-05 23:37:04 +01:00
return $this->tokenService->getDatatable(Auth::user()->account_id);
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);
}
public function update($publicId)
{
return $this->save($publicId);
}
public function store()
{
return $this->save();
}
/**
* Displays the form for account creation
*
*/
public function create()
{
$data = [
'token' => null,
'method' => 'POST',
'url' => 'tokens',
'title' => trans('texts.add_token'),
];
return View::make('accounts.token', $data);
}
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
}
/**
* Stores new account
*
*/
public function save($tokenPublicId = false)
{
if (Auth::user()->account->isPro()) {
$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'));
$token->token = str_random(RANDOM_KEY_LENGTH);
}
$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
}
}