daily pastebin goal
21%
SHARE
TWEET

Untitled

a guest Apr 27th, 2018 250 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Ability
  2.   include CanCan::Ability
  3.  
  4.   # alias_action :index, :show, :to => :read
  5.   # alias_action :new, :to => :create
  6.   # alias_action :edit, :to => :update
  7.  
  8.   def initialize(user)
  9.     user ||= login_as_trial_user
  10.    
  11.     if user.role? :admin
  12.       can :manage, :all
  13.     end
  14.    
  15.     if user.role? :moderator
  16.       can :manage, :all
  17.     end
  18.    
  19.     if user.role? :user
  20.       can :read, :all
  21.       can :create, :all
  22.       can :destroy, :all do |obj_class, obj|
  23.        obj.try(:user) == user
  24.        3.minutes.ago <= obj.created_at
  25.      end
  26.       can :update, :all do |obj_class, obj|
  27.        obj.try(:user) == user
  28.        3.minutes.ago <= obj.created_at
  29.       end
  30.       can :destroy, UserSession do |us|
  31.         user == us.try(:user)
  32.       end
  33.       cannot :create, UserSession
  34.       cannot :create, User
  35.       cannot :destroy, User
  36.     end
  37.    
  38.     if user.role? :guest
  39.       can :read, :all
  40.       can :create, UserSession
  41.       cannot :destroy, UserSession
  42.       can :create, User
  43.     end
  44.    
  45.   end
  46.  
  47.   # private
  48.  
  49.   def login_as_trial_user
  50.     name = "anonymous_#{Time.now.to_i * rand}"
  51.     if User.find_by_username(name)
  52.       UserSession.create(User.find_by_username(name),true)
  53.     else
  54.       guest_role = User.create(:username => name, :password => name, :password_confirmation => name, :role => "guest", :email => "change@this.com")
  55.       UserSession.create(guest_role, true)
  56.     end
  57.     @current_user_session = UserSession.find
  58.     guest_role
  59.   end
  60.  
  61. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top