User tablomuza bool türünde alan ekleyelim.
$ rails generate migration AddIsAktiveToUsers isAktif:boolean
seed.rb:
user=User.new
user.email="super@gmail.com"
user.password = '1q2w3e4r'
user.password_confirmation = '1q2w3e4r'
user.roles<<Role.where(role: "SuperAdmin")
user.isAktif="true"
user.save
Kullanıcı giriş yaptığında aktifse girsin değilse giriş ekranına yönelsin:
session_controller.rb olusturalım ve create kısmını aşağıdaki gibi düzenleyelim.
def create
@user=User.new(login_params)
@us=User.where(["email= ?",@user.email]).first
if(@us!=nil)
if(@us.isAktif==true)
self.resource = warden.authenticate!(auth_options)
set_flash_message(:notice, :signed_in) if is_flashing_format?
sign_in(resource_name, resource)
yield resource if block_given?
respond_with resource, location: after_sign_in_path_for(resource)
else
redirect_to new_user_session_path, error: "Hesabınız Aktif Değil"
end
else
redirect_to new_user_registration_path
end
end
application_controller.rb
add_flash_types :error
view/devise/sessions/new.html.erb
<%= error %>
esra@gmail hesabını console c ile isaktif alanını false yapalım ve sisteme girelim
super admin olan yetkili diğer kullanıcıların hesaplarını aktif veya pasif edebilsin
controller/users_controller.rb olusturup aşağıdaki verileri girelim
class UsersController < ApplicationController
before_action :authenticate_user!
def index
@uyeler = User.where("email!=?",current_user.email).all
end
def active_pasive
@user=User.find(params["id"])
if(@user.Aktif==true)
@user.Aktif=false
elsif (@user.Aktif==false)
@user.Aktif=true
end
@user.save
redirect_to users_path
end
end
policies/user_policies.rb oluşturup aşağıdaki verileri girelim
class UserPolicy < ApplicationPolicy
attr_reader :user, :uye
def initialize(user, uye)
@user = user
@uye = uye
end
def active_pasive?
uye.roles.where(role: 'SuperAdmin').exists?
end
end
routes.rb
resources :users do
get :active_pasive, on: :member
end
view/users/index.html.slim oluşturup aşağıdaki kodları yazalım
table
thead
tr
th email
th Yetkisi
th Aktif
th
tbody
-@uyeler.each do |user|
tr
td = user.email
td = user.roles.first.role
td = user.Aktif
td =link_to "A/P" ,active_pasive_user_path(user)
çalıştırıp bakalım