|
@@ -1,6 +1,7 @@
|
1
|
1
|
class Api::V1::UsersController < ApplicationController
|
2
|
2
|
before_action :set_user, only: %i[show update destroy]
|
3
|
|
- before_action :check_owner, only: %i[update destroy]
|
|
3
|
+ before_action :check_login, only: %i[index show]
|
|
4
|
+ before_action :check_owner_or_admin, only: %i[update destroy]
|
4
|
5
|
|
5
|
6
|
def index
|
6
|
7
|
render json: UserSerializer.new(User.all).serializable_hash.to_json
|
|
@@ -42,14 +43,18 @@ class Api::V1::UsersController < ApplicationController
|
42
|
43
|
|
43
|
44
|
# Only allow a trusted parameter "white list" through.
|
44
|
45
|
def user_params
|
45
|
|
- params.require(:user).permit(:email, :username, :password)
|
|
46
|
+ if current_user&.admin
|
|
47
|
+ params.require(:user).permit(:email, :username, :password, :admin)
|
|
48
|
+ else
|
|
49
|
+ params.require(:user).permit(:email, :username, :password)
|
|
50
|
+ end
|
46
|
51
|
end
|
47
|
52
|
|
48
|
53
|
def set_user
|
49
|
54
|
@user = User.find(params[:id])
|
50
|
55
|
end
|
51
|
56
|
|
52
|
|
- def check_owner
|
53
|
|
- head :forbidden unless @user.id == current_user&.id
|
|
57
|
+ def check_owner_or_admin
|
|
58
|
+ head :forbidden unless @user.id == current_user&.id || current_user.admin
|
54
|
59
|
end
|
55
|
60
|
end
|