]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/UserApiTokenController.php
move zip export into exportservice
[bookstack] / app / Http / Controllers / UserApiTokenController.php
1 <?php namespace BookStack\Http\Controllers;
2
3 use BookStack\Api\ApiToken;
4 use BookStack\Auth\User;
5 use Illuminate\Http\Request;
6 use Illuminate\Support\Carbon;
7 use Illuminate\Support\Facades\Hash;
8 use Illuminate\Support\Str;
9
10 class UserApiTokenController extends Controller
11 {
12
13     /**
14      * Show the form to create a new API token.
15      */
16     public function create(int $userId)
17     {
18         // Ensure user is has access-api permission and is the current user or has permission to manage the current user.
19         $this->checkPermission('access-api');
20         $this->checkPermissionOrCurrentUser('users-manage', $userId);
21
22         $user = User::query()->findOrFail($userId);
23         return view('users.api-tokens.create', [
24             'user' => $user,
25         ]);
26     }
27
28     /**
29      * Store a new API token in the system.
30      */
31     public function store(Request $request, int $userId)
32     {
33         $this->checkPermission('access-api');
34         $this->checkPermissionOrCurrentUser('users-manage', $userId);
35
36         $this->validate($request, [
37             'name' => 'required|max:250',
38             'expires_at' => 'date_format:Y-m-d',
39         ]);
40
41         $user = User::query()->findOrFail($userId);
42         $secret = Str::random(32);
43
44         $token = (new ApiToken())->forceFill([
45             'name' => $request->get('name'),
46             'token_id' => Str::random(32),
47             'secret' => Hash::make($secret),
48             'user_id' => $user->id,
49             'expires_at' => $request->get('expires_at') ?: ApiToken::defaultExpiry(),
50         ]);
51
52         while (ApiToken::query()->where('token_id', '=', $token->token_id)->exists()) {
53             $token->token_id = Str::random(32);
54         }
55
56         $token->save();
57
58         session()->flash('api-token-secret:' . $token->id, $secret);
59         $this->showSuccessNotification(trans('settings.user_api_token_create_success'));
60         return redirect($user->getEditUrl('/api-tokens/' . $token->id));
61     }
62
63     /**
64      * Show the details for a user API token, with access to edit.
65      */
66     public function edit(int $userId, int $tokenId)
67     {
68         [$user, $token] = $this->checkPermissionAndFetchUserToken($userId, $tokenId);
69         $secret = session()->pull('api-token-secret:' . $token->id, null);
70
71         return view('users.api-tokens.edit', [
72             'user' => $user,
73             'token' => $token,
74             'model' => $token,
75             'secret' => $secret,
76         ]);
77     }
78
79     /**
80      * Update the API token.
81      */
82     public function update(Request $request, int $userId, int $tokenId)
83     {
84         $this->validate($request, [
85             'name' => 'required|max:250',
86             'expires_at' => 'date_format:Y-m-d',
87         ]);
88
89         [$user, $token] = $this->checkPermissionAndFetchUserToken($userId, $tokenId);
90         $token->fill([
91             'name' => $request->get('name'),
92             'expires_at' => $request->get('expires_at') ?: ApiToken::defaultExpiry(),
93         ])->save();
94
95         $this->showSuccessNotification(trans('settings.user_api_token_update_success'));
96         return redirect($user->getEditUrl('/api-tokens/' . $token->id));
97     }
98
99     /**
100      * Show the delete view for this token.
101      */
102     public function delete(int $userId, int $tokenId)
103     {
104         [$user, $token] = $this->checkPermissionAndFetchUserToken($userId, $tokenId);
105         return view('users.api-tokens.delete', [
106             'user' => $user,
107             'token' => $token,
108         ]);
109     }
110
111     /**
112      * Destroy a token from the system.
113      */
114     public function destroy(int $userId, int $tokenId)
115     {
116         [$user, $token] = $this->checkPermissionAndFetchUserToken($userId, $tokenId);
117         $token->delete();
118
119         $this->showSuccessNotification(trans('settings.user_api_token_delete_success'));
120         return redirect($user->getEditUrl('#api_tokens'));
121     }
122
123     /**
124      * Check the permission for the current user and return an array
125      * where the first item is the user in context and the second item is their
126      * API token in context.
127      */
128     protected function checkPermissionAndFetchUserToken(int $userId, int $tokenId): array
129     {
130         $this->checkPermissionOr('users-manage', function () use ($userId) {
131             return $userId === user()->id && userCan('access-api');
132         });
133
134         $user = User::query()->findOrFail($userId);
135         $token = ApiToken::query()->where('user_id', '=', $user->id)->where('id', '=', $tokenId)->firstOrFail();
136         return [$user, $token];
137     }
138
139 }