1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-08 20:22:42 +01:00

Added users to the API

This commit is contained in:
Hillel Coren 2016-02-04 22:35:28 +02:00
parent 60a3f3ca28
commit 88b9046ac7
6 changed files with 132 additions and 2 deletions

View File

@ -0,0 +1,76 @@
<?php namespace App\Http\Controllers;
use App\Services\UserService;
use App\Ninja\Repositories\UserRepository;
use App\Ninja\Transformers\UserTransformer;
use Auth;
use App\Models\User;
use App\Http\Requests\CreateUserRequest;
use App\Http\Requests\UpdateUserRequest;
class UserApiController extends BaseAPIController
{
protected $userService;
protected $userRepo;
public function __construct(UserService $userService, UserRepository $userRepo)
{
parent::__construct();
$this->userService = $userService;
$this->userRepo = $userRepo;
}
public function index()
{
$user = Auth::user();
$users = User::whereAccountId($user->account_id)->withTrashed();
$users = $users->paginate();
$paginator = User::whereAccountId($user->account_id)->withTrashed()->paginate();
$transformer = new UserTransformer(Auth::user()->account, $this->serializer);
$data = $this->createCollection($users, $transformer, 'users', $paginator);
return $this->response($data);
}
/*
public function store(CreateUserRequest $request)
{
return $this->save($request);
}
*/
public function update(UpdateUserRequest $request, $userPublicId)
{
/*
// temporary fix for ids starting at 0
$userPublicId -= 1;
$user = User::scope($userPublicId)->firstOrFail();
*/
$user = Auth::user();
if ($request->action == ACTION_ARCHIVE) {
$this->userRepo->archive($user);
$transformer = new UserTransformer(Auth::user()->account, $request->serializer);
$data = $this->createItem($user, $transformer, 'users');
return $this->response($data);
} else {
return $this->save($request, $user);
}
}
private function save($request, $user = false)
{
$user = $this->userRepo->save($request->input(), $user);
$transformer = new UserTransformer(\Auth::user()->account, $request->serializer);
$data = $this->createItem($user, $transformer, 'users');
return $this->response($data);
}
}

View File

@ -0,0 +1,33 @@
<?php namespace app\Http\Requests;
use Auth;
use App\Http\Requests\Request;
use Illuminate\Validation\Factory;
class UpdateUserRequest extends Request
{
// Expenses
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'email' => 'email|required|unique:users,email,' . Auth::user()->id . ',id',
'first_name' => 'required',
'last_name' => 'required',
];
}
}

View File

@ -235,6 +235,7 @@ Route::group(['middleware' => 'api', 'prefix' => 'api/v1'], function()
Route::get('user_accounts', 'AccountApiController@getUserAccounts');
Route::resource('products', 'ProductApiController');
Route::resource('tax_rates', 'TaxRateApiController');
Route::resource('users', 'UserApiController');
// Vendor
Route::resource('vendors', 'VendorApiController');

View File

@ -29,7 +29,13 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon
*
* @var array
*/
protected $fillable = ['first_name', 'last_name', 'email', 'password'];
protected $fillable = [
'first_name',
'last_name',
'email',
'password',
'phone',
];
/**
* The attributes excluded from the model's JSON form.
@ -51,6 +57,11 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon
return $this->belongsTo('App\Models\Theme');
}
public function setEmailAttribute($value)
{
$this->attributes['email'] = $this->attributes['username'] = $value;
}
public function getName()
{
return $this->getDisplayName();

View File

@ -26,4 +26,13 @@ class UserRepository extends BaseRepository
return $query;
}
public function save($data, $user)
{
$user->fill($data);
$user->save();
return $user;
}
}

View File

@ -17,7 +17,7 @@ class UserTransformer extends EntityTransformer
'updated_at' => $this->getTimestamp($user->updated_at),
'deleted_at' => $this->getTimestamp($user->deleted_at),
'phone' => $user->phone,
'username' => $user->username,
//'username' => $user->username,
'registered' => (bool) $user->registered,
'confirmed' => (bool) $user->confirmed,
'oauth_user_id' => $user->oauth_user_id,