Changeset View
Changeset View
Standalone View
Standalone View
src/app/Http/Controllers/API/UsersController.php
Show First 20 Lines • Show All 69 Lines • ▼ Show 20 Lines | class UsersController extends Controller | ||||
/** | /** | ||||
* Get the authenticated User | * Get the authenticated User | ||||
* | * | ||||
* @return \Illuminate\Http\JsonResponse | * @return \Illuminate\Http\JsonResponse | ||||
*/ | */ | ||||
public function info() | public function info() | ||||
{ | { | ||||
$user = $this->guard()->user(); | $user = $this->guard()->user(); | ||||
$response = $user->toArray(); | $response = $this->userResponse($user); | ||||
// Settings | |||||
// TODO: It might be reasonable to limit the list of settings here to these | |||||
// that are safe and are used in the UI | |||||
$response['settings'] = []; | |||||
foreach ($user->settings as $item) { | |||||
$response['settings'][$item->key] = $item->value; | |||||
} | |||||
// Status info | |||||
$response['statusInfo'] = self::statusInfo($user); | |||||
return response()->json($response); | return response()->json($response); | ||||
} | } | ||||
/** | /** | ||||
* Get a JWT token via given credentials. | * Get a JWT token via given credentials. | ||||
* | * | ||||
* @param \Illuminate\Http\Request $request The API request. | * @param \Illuminate\Http\Request $request The API request. | ||||
Show All 9 Lines | public function login(Request $request) | ||||
'password' => 'required|min:4', | 'password' => 'required|min:4', | ||||
] | ] | ||||
); | ); | ||||
if ($v->fails()) { | if ($v->fails()) { | ||||
return response()->json(['status' => 'error', 'errors' => $v->errors()], 422); | return response()->json(['status' => 'error', 'errors' => $v->errors()], 422); | ||||
} | } | ||||
$credentials = $request->only('email', 'password'); | $credentials = $request->only('email', 'password'); | ||||
if ($token = $this->guard()->attempt($credentials)) { | if ($token = $this->guard()->attempt($credentials)) { | ||||
return $this->respondWithToken($token); | return $this->respondWithToken($token); | ||||
} | } | ||||
return response()->json(['status' => 'error', 'message' => __('auth.failed')], 401); | return response()->json(['status' => 'error', 'message' => __('auth.failed')], 401); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 52 Lines • ▼ Show 20 Lines | class UsersController extends Controller | ||||
{ | { | ||||
if (!$this->hasAccess($id)) { | if (!$this->hasAccess($id)) { | ||||
return $this->errorResponse(403); | return $this->errorResponse(403); | ||||
} | } | ||||
$user = User::find($id); | $user = User::find($id); | ||||
if (empty($user)) { | if (empty($user)) { | ||||
return $this->errorResponse(404); | return $this->errorResponse(404); | ||||
} | } | ||||
return response()->json($user); | $response = $this->userResponse($user); | ||||
return response()->json($response); | |||||
} | } | ||||
/** | /** | ||||
* User status (extended) information | * User status (extended) information | ||||
* | * | ||||
* @param \App\User $user User object | * @param \App\User $user User object | ||||
* | * | ||||
* @return array Status information | * @return array Status information | ||||
▲ Show 20 Lines • Show All 144 Lines • ▼ Show 20 Lines | class UsersController extends Controller | ||||
{ | { | ||||
$current_user = $this->guard()->user(); | $current_user = $this->guard()->user(); | ||||
// TODO: Admins, other users | // TODO: Admins, other users | ||||
// FIXME: This probably should be some kind of middleware/guard | // FIXME: This probably should be some kind of middleware/guard | ||||
return $current_user->id == $user_id; | return $current_user->id == $user_id; | ||||
} | } | ||||
/** | |||||
* Create a response data array for specified user. | |||||
* | |||||
* @param \App\User $user User object | |||||
* | |||||
* @return array Response data | |||||
*/ | |||||
protected function userResponse(User $user): array | |||||
{ | |||||
$response = $user->toArray(); | |||||
// Settings | |||||
// TODO: It might be reasonable to limit the list of settings here to these | |||||
// that are safe and are used in the UI | |||||
$response['settings'] = []; | |||||
foreach ($user->settings as $item) { | |||||
$response['settings'][$item->key] = $item->value; | |||||
} | |||||
// Status info | |||||
$response['statusInfo'] = self::statusInfo($user); | |||||
return $response; | |||||
} | |||||
} | } |