From c6471297f0a871ba1062f1285790d39e5471b966 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Wed, 5 Mar 2014 22:38:44 -0500 Subject: users_controller: check permissions for doing things --- app/controllers/users_controller.rb | 22 +++++++++++++++++++++- app/views/application/permission_denied.html.erb | 1 + app/views/users/already_signed_in.html.erb | 1 + 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 app/views/application/permission_denied.html.erb create mode 100644 app/views/users/already_signed_in.html.erb diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 284892b..907958b 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,5 +1,7 @@ class UsersController < ApplicationController before_action :set_user, only: [:show, :edit, :update, :destroy] + before_action :perms_edit, only: [:edit, :update, :destroy] + before_action :perms_create, only: [:new, :create] # GET /users # GET /users.json @@ -33,7 +35,7 @@ class UsersController < ApplicationController format.html { redirect_to root_path, notice: 'User was successfully created.' } format.json { render action: 'show', status: :created, location: @user } else - format.html { render action: 'new' } + format.html { render action: 'new', status: :unprocessable_entity } format.json { render json: @user.errors, status: :unprocessable_entity } end end @@ -69,6 +71,24 @@ class UsersController < ApplicationController @user = User.find(params[:id]) end + def perms_edit + unless (current_user == @user) or (signed_in? and current_user.in_group? :admin) + respond_to do |format| + format.html { render action: 'permission_denied', status: :forbidden } + format.json { render json: "Permission denied", status: :forbidden } + end + end + end + + def perms_create + if signed_in? + respond_to do |format| + format.html { render action: 'already_signed_in', status: :unprocessable_entity } + format.json { render json: "Already signed in", status: :unprocessable_entity } + end + end + end + # Never trust parameters from the scary internet, only allow the white list through. def user_params params.require(:user).permit(:name, :email, :user_name, :password, :password_confirmation) diff --git a/app/views/application/permission_denied.html.erb b/app/views/application/permission_denied.html.erb new file mode 100644 index 0000000..1ef883c --- /dev/null +++ b/app/views/application/permission_denied.html.erb @@ -0,0 +1 @@ +

Permission denied

diff --git a/app/views/users/already_signed_in.html.erb b/app/views/users/already_signed_in.html.erb new file mode 100644 index 0000000..04b4248 --- /dev/null +++ b/app/views/users/already_signed_in.html.erb @@ -0,0 +1 @@ +

You are currently signed in

-- cgit v1.2.3