Guest User

Untitled

a guest
Apr 26th, 2018
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  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 ||= require_login
  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 require_login
  50. # login_as_trial_user unless current_user_session
  51. # end
  52. #
  53. # def login_as_trial_user
  54. # #I don't want to use the method form here, I want it in app_controller, and I want using session[:session_id]
  55. # #instead of rand and shit
  56. # name = "anonymous_#{rand}_#{Time.now}_#{rand}"
  57. # if User.find_by_username(name)
  58. # UserSession.create(User.find_by_username(name),true)
  59. # else
  60. # guest_role = User.create(:username => name, :password => name, :password_confirmation => name, :role => "guest", :email => "change@this.com")
  61. # UserSession.create(guest_role, true)
  62. # end
  63. # @current_user_session = UserSession.find
  64. # guest_role
  65. # end
  66.  
  67. end
Add Comment
Please, Sign In to add comment