Commit b0d50fc7 by Mykhailo Makohin

add abilities to users

parent 42ccc689
...@@ -25,6 +25,8 @@ gem 'gmaps4rails' ...@@ -25,6 +25,8 @@ gem 'gmaps4rails'
gem 'remotipart' gem 'remotipart'
gem 'simple_form' gem 'simple_form'
gem 'activeadmin' gem 'activeadmin'
gem 'carrierwave'
gem 'cancancan'
group :development, :test do group :development, :test do
gem 'byebug', platform: :mri gem 'byebug', platform: :mri
......
...@@ -50,6 +50,8 @@ GEM ...@@ -50,6 +50,8 @@ GEM
i18n (>= 0.7, < 2) i18n (>= 0.7, < 2)
minitest (~> 5.1) minitest (~> 5.1)
tzinfo (~> 1.1) tzinfo (~> 1.1)
addressable (2.7.0)
public_suffix (>= 2.0.2, < 5.0)
arbre (1.2.1) arbre (1.2.1)
activesupport (>= 3.0.0) activesupport (>= 3.0.0)
arel (7.1.4) arel (7.1.4)
...@@ -66,6 +68,14 @@ GEM ...@@ -66,6 +68,14 @@ GEM
sassc (>= 2.0.0) sassc (>= 2.0.0)
builder (3.2.3) builder (3.2.3)
byebug (11.0.1) byebug (11.0.1)
cancancan (3.0.1)
carrierwave (2.0.1)
activemodel (>= 5.0.0)
activesupport (>= 5.0.0)
addressable (~> 2.6)
image_processing (~> 1.1)
mimemagic (>= 0.3.0)
mini_mime (>= 0.1.3)
coffee-rails (4.2.2) coffee-rails (4.2.2)
coffee-script (>= 2.2.0) coffee-script (>= 2.2.0)
railties (>= 4.0.0) railties (>= 4.0.0)
...@@ -75,7 +85,7 @@ GEM ...@@ -75,7 +85,7 @@ GEM
coffee-script-source (1.12.2) coffee-script-source (1.12.2)
concurrent-ruby (1.1.5) concurrent-ruby (1.1.5)
crass (1.0.4) crass (1.0.4)
devise (4.7.0) devise (4.7.1)
bcrypt (~> 3.0) bcrypt (~> 3.0)
orm_adapter (~> 0.1) orm_adapter (~> 0.1)
railties (>= 4.1.0) railties (>= 4.1.0)
...@@ -101,6 +111,9 @@ GEM ...@@ -101,6 +111,9 @@ GEM
hashie (3.6.0) hashie (3.6.0)
i18n (1.6.0) i18n (1.6.0)
concurrent-ruby (~> 1.0) concurrent-ruby (~> 1.0)
image_processing (1.9.3)
mini_magick (>= 4.9.5, < 5)
ruby-vips (>= 2.0.13, < 3)
inherited_resources (1.10.0) inherited_resources (1.10.0)
actionpack (>= 5.0, < 6.0) actionpack (>= 5.0, < 6.0)
has_scope (~> 0.6) has_scope (~> 0.6)
...@@ -134,6 +147,8 @@ GEM ...@@ -134,6 +147,8 @@ GEM
mail (2.7.1) mail (2.7.1)
mini_mime (>= 0.1.1) mini_mime (>= 0.1.1)
method_source (0.9.2) method_source (0.9.2)
mimemagic (0.3.3)
mini_magick (4.9.5)
mini_mime (1.0.2) mini_mime (1.0.2)
mini_portile2 (2.4.0) mini_portile2 (2.4.0)
minitest (5.11.3) minitest (5.11.3)
...@@ -141,7 +156,7 @@ GEM ...@@ -141,7 +156,7 @@ GEM
multi_xml (0.6.0) multi_xml (0.6.0)
multipart-post (2.1.1) multipart-post (2.1.1)
mysql2 (0.5.2) mysql2 (0.5.2)
nio4r (2.5.0) nio4r (2.5.1)
nokogiri (1.10.4) nokogiri (1.10.4)
mini_portile2 (~> 2.4.0) mini_portile2 (~> 2.4.0)
oauth2 (1.4.1) oauth2 (1.4.1)
...@@ -167,7 +182,10 @@ GEM ...@@ -167,7 +182,10 @@ GEM
orm_adapter (0.5.0) orm_adapter (0.5.0)
polyamorous (2.3.0) polyamorous (2.3.0)
activerecord (>= 5.0) activerecord (>= 5.0)
public_suffix (4.0.1)
puma (3.12.1) puma (3.12.1)
pundit (2.1.0)
activesupport (>= 3.0.0)
rack (2.0.7) rack (2.0.7)
rack-test (0.6.3) rack-test (0.6.3)
rack (>= 1.0) rack (>= 1.0)
...@@ -208,6 +226,8 @@ GEM ...@@ -208,6 +226,8 @@ GEM
responders (2.4.1) responders (2.4.1)
actionpack (>= 4.2.0, < 6.0) actionpack (>= 4.2.0, < 6.0)
railties (>= 4.2.0, < 6.0) railties (>= 4.2.0, < 6.0)
ruby-vips (2.0.15)
ffi (~> 1.9)
sass (3.7.4) sass (3.7.4)
sass-listen (~> 4.0.0) sass-listen (~> 4.0.0)
sass-listen (4.0.0) sass-listen (4.0.0)
...@@ -269,6 +289,8 @@ DEPENDENCIES ...@@ -269,6 +289,8 @@ DEPENDENCIES
activeadmin activeadmin
bootstrap-sass bootstrap-sass
byebug byebug
cancancan
carrierwave
coffee-rails (~> 4.2) coffee-rails (~> 4.2)
devise devise
gmaps4rails gmaps4rails
...@@ -282,6 +304,7 @@ DEPENDENCIES ...@@ -282,6 +304,7 @@ DEPENDENCIES
omniauth-google-oauth2 omniauth-google-oauth2
omniauth-linkedin-oauth2 omniauth-linkedin-oauth2
puma (~> 3.0) puma (~> 3.0)
pundit
rails (~> 5.0.7, >= 5.0.7.2) rails (~> 5.0.7, >= 5.0.7.2)
remotipart remotipart
sass-rails (~> 5.0) sass-rails (~> 5.0)
......
...@@ -28,5 +28,16 @@ ActiveAdmin.register_page "Dashboard" do ...@@ -28,5 +28,16 @@ ActiveAdmin.register_page "Dashboard" do
# end # end
# end # end
# end # end
controller do
# before_action :authenticate_user!
load_and_authorize_resource
def index
puts '============================'
puts current_user.present?
puts current_user.admin?
puts '============================'
end
end
end # content end # content
end end
ActiveAdmin.register User do ActiveAdmin.register User do
permit_params :email, :role, :name, :location filter :email
filter :name
filter :location
filter :created_at
filter :updated_at
filter :role
permit_params :email, :role, :name, :location, :avatar
index do
selectable_column
id_column
column :email
column :name
column :location
column :created_at
column :updated_at
column :role
actions
end
show title: :name do
panel "User Details" do
attributes_table_for user, :email, :name, :location, :created_at,
:updated_at, :role
end
end
form do |f|
f.inputs do
f.input :email
f.input :name
f.input :location
f.input :password
f.input :password_confirmation
f.input :role
f.input :avatar
end
f.actions
end
end end
class ApplicationController < ActionController::Base class ApplicationController < ActionController::Base
protect_from_forgery with: :exception protect_from_forgery with: :exception
before_action :set_locale
def set_locale def set_locale
I18n.locale = params[:locale] || I18n.default_locale I18n.locale = params[:locale] || I18n.default_locale
...@@ -10,4 +9,12 @@ class ApplicationController < ActionController::Base ...@@ -10,4 +9,12 @@ class ApplicationController < ActionController::Base
{ locale: I18n.locale } { locale: I18n.locale }
end end
def authenticate_user!
redirect_to root_path, notice: 'Для подальшої роботи з системою Вам потрібно авторизуватися' unless user_signed_in?
end
def access_denied(exception)
redirect_to root_path, alert: exception.message
end
end end
class HomeController < ApplicationController class HomeController < ApplicationController
def index; end def index
puts current_user.present?
end
end end
\ No newline at end of file
class UsersController < ApplicationController class UsersController < ApplicationController
load_and_authorize_resource
def index def index
@users = collection @users = collection
...@@ -52,7 +53,7 @@ class UsersController < ApplicationController ...@@ -52,7 +53,7 @@ class UsersController < ApplicationController
end end
def user_params def user_params
params.require(:user).permit(:email, :name, :location, :picture) params.require(:user).permit(:email, :name, :location, :picture, :avatar)
end end
end end
\ No newline at end of file
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new # guest user (not logged in)
alias_action :create, :read, :update, :destroy, to: :crud
if user.admin?
can :read, ActiveAdmin::Page, :name => "Dashboard"
end
case
when user.admin?
#Dashboard
can :index, :home
# User related abilities
can :manage, User
end
end
end
class User < ApplicationRecord class User < ApplicationRecord
attr_accessor :skip_password_validation attr_accessor :skip_password_validation
mount_uploader :avatar, AvatarUploader
enum role: [:user, :admin] enum role: [:user, :admin]
after_initialize :set_default_role, :if => :new_record? after_initialize :set_default_role, :if => :new_record?
...@@ -39,4 +42,5 @@ class User < ApplicationRecord ...@@ -39,4 +42,5 @@ class User < ApplicationRecord
return false if skip_password_validation return false if skip_password_validation
super super
end end
end end
class AvatarUploader < CarrierWave::Uploader::Base
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
...@@ -48,7 +48,10 @@ ...@@ -48,7 +48,10 @@
#sign_in #sign_in
.dropdown.user_dropdown .dropdown.user_dropdown
%button#dropdownMenu1.dropdown-toggle{"aria-expanded" => "false", "aria-haspopup" => "true", "data-toggle" => "dropdown", :type => "button"} %button#dropdownMenu1.dropdown-toggle{"aria-expanded" => "false", "aria-haspopup" => "true", "data-toggle" => "dropdown", :type => "button"}
%img{:alt => "Logo", :src => "", :width => "20"}/ - if current_user.avatar?
= image_tag(current_user.avatar_url)
- else
= image_tag "https://cdn1.iconfinder.com/data/icons/rcons-user-action/512/user-512.png"
%ul.dropdown-menu{"aria-labelledby" => "dropdownMenu1"} %ul.dropdown-menu{"aria-labelledby" => "dropdownMenu1"}
- if current_user.admin? - if current_user.admin?
%li %li
......
...@@ -2,6 +2,8 @@ require_relative 'boot' ...@@ -2,6 +2,8 @@ require_relative 'boot'
require 'rails/all' require 'rails/all'
require 'carrierwave'
# Require the gems listed in Gemfile, including any gems # Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production. # you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups) Bundler.require(*Rails.groups)
......
...@@ -6,6 +6,16 @@ ActiveAdmin.setup do |config| ...@@ -6,6 +6,16 @@ ActiveAdmin.setup do |config|
# #
config.site_title = "Warm City" config.site_title = "Warm City"
# config.authorization_adapter = ActiveAdmin::CanCanAdapter
# config.on_unauthorized_access = :access_denied
config.authorization_adapter = ActiveAdmin::CanCanAdapter
config.cancan_ability_class = Ability
config.authentication_method = :authenticate_user!
# Set the link url for the title. For example, to take # Set the link url for the title. For example, to take
# users to your main site. Defaults to no link. # users to your main site. Defaults to no link.
# #
...@@ -91,7 +101,7 @@ ActiveAdmin.setup do |config| ...@@ -91,7 +101,7 @@ ActiveAdmin.setup do |config|
# #
# This setting changes the method which Active Admin calls # This setting changes the method which Active Admin calls
# (within the application controller) to return the currently logged in user. # (within the application controller) to return the currently logged in user.
# config.current_user_method = :current_admin_user config.current_user_method = :current_user
# == Logging Out # == Logging Out
# #
...@@ -103,7 +113,7 @@ ActiveAdmin.setup do |config| ...@@ -103,7 +113,7 @@ ActiveAdmin.setup do |config|
# will call the method to return the path. # will call the method to return the path.
# #
# Default: # Default:
config.logout_link_path = :destroy_admin_user_session_path config.logout_link_path = :destroy_user_session_path
# This setting changes the http method used when rendering the # This setting changes the http method used when rendering the
# link. For example :get, :delete, :put, etc.. # link. For example :get, :delete, :put, etc..
......
...@@ -4,6 +4,6 @@ class AddOmniauthToUsers < ActiveRecord::Migration[5.0] ...@@ -4,6 +4,6 @@ class AddOmniauthToUsers < ActiveRecord::Migration[5.0]
add_column :users, :uid, :string add_column :users, :uid, :string
add_column :users, :name, :string add_column :users, :name, :string
add_column :users, :location, :string add_column :users, :location, :string
add_column :users, :picture, :text add_column :users, :avatar, :string
end end
end end
...@@ -38,7 +38,7 @@ ActiveRecord::Schema.define(version: 20190911111638) do ...@@ -38,7 +38,7 @@ ActiveRecord::Schema.define(version: 20190911111638) do
t.string "uid" t.string "uid"
t.string "name" t.string "name"
t.string "location" t.string "location"
t.text "picture", limit: 65535 t.string "avatar"
t.integer "role" t.integer "role"
t.index ["email"], name: "index_users_on_email", unique: true, using: :btree t.index ["email"], name: "index_users_on_email", unique: true, using: :btree
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment