mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
DEV: Improve Guardian devex (#24706)
It's quite confusing for blank? to be overridden on AnonymousUser and BasicUser to represent whether the fake user is authenticated or not; we can achieve the same thing more clearly with a wrapper GuardianUser class around these user classes. Also fixes an issue where `def user` would be returning nil.
This commit is contained in:
parent
47629db3db
commit
77b6a038ba
@ -11,6 +11,36 @@ require "guardian/tag_guardian"
|
|||||||
require "guardian/topic_guardian"
|
require "guardian/topic_guardian"
|
||||||
require "guardian/user_guardian"
|
require "guardian/user_guardian"
|
||||||
|
|
||||||
|
class GuardianUser
|
||||||
|
def initialize(user_alike)
|
||||||
|
@user_alike = user_alike
|
||||||
|
end
|
||||||
|
|
||||||
|
def actual
|
||||||
|
@user_alike
|
||||||
|
end
|
||||||
|
|
||||||
|
def fake?
|
||||||
|
if @user_alike.respond_to?(:fake?)
|
||||||
|
@user_alike.fake?
|
||||||
|
else
|
||||||
|
false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def authenticated?
|
||||||
|
if @user_alike.respond_to?(:authenticated?)
|
||||||
|
@user_alike.authenticated?
|
||||||
|
else
|
||||||
|
true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def method_missing(method, *args, &block)
|
||||||
|
@user_alike.public_send(method, *args, &block)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# The guardian is responsible for confirming access to various site resources and operations
|
# The guardian is responsible for confirming access to various site resources and operations
|
||||||
class Guardian
|
class Guardian
|
||||||
include BookmarkGuardian
|
include BookmarkGuardian
|
||||||
@ -28,6 +58,12 @@ class Guardian
|
|||||||
def blank?
|
def blank?
|
||||||
true
|
true
|
||||||
end
|
end
|
||||||
|
def fake?
|
||||||
|
true
|
||||||
|
end
|
||||||
|
def authenticated?
|
||||||
|
false
|
||||||
|
end
|
||||||
def admin?
|
def admin?
|
||||||
false
|
false
|
||||||
end
|
end
|
||||||
@ -84,7 +120,13 @@ class Guardian
|
|||||||
# categories or PMs but can read public topics.
|
# categories or PMs but can read public topics.
|
||||||
class BasicUser
|
class BasicUser
|
||||||
def blank?
|
def blank?
|
||||||
false
|
true
|
||||||
|
end
|
||||||
|
def fake?
|
||||||
|
true
|
||||||
|
end
|
||||||
|
def authenticated?
|
||||||
|
true
|
||||||
end
|
end
|
||||||
def admin?
|
def admin?
|
||||||
false
|
false
|
||||||
@ -139,7 +181,8 @@ class Guardian
|
|||||||
attr_reader :request
|
attr_reader :request
|
||||||
|
|
||||||
def initialize(user = nil, request = nil)
|
def initialize(user = nil, request = nil)
|
||||||
@user = user.presence || AnonymousUser.new
|
@guardian_user = GuardianUser.new(user.presence || AnonymousUser.new)
|
||||||
|
@user = @guardian_user.actual
|
||||||
@request = request
|
@request = request
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -152,7 +195,7 @@ class Guardian
|
|||||||
end
|
end
|
||||||
|
|
||||||
def user
|
def user
|
||||||
@user.presence
|
@guardian_user.fake? ? nil : @user
|
||||||
end
|
end
|
||||||
alias current_user user
|
alias current_user user
|
||||||
|
|
||||||
@ -161,7 +204,7 @@ class Guardian
|
|||||||
end
|
end
|
||||||
|
|
||||||
def authenticated?
|
def authenticated?
|
||||||
@user.present?
|
@guardian_user.authenticated?
|
||||||
end
|
end
|
||||||
|
|
||||||
def is_admin?
|
def is_admin?
|
||||||
|
Loading…
Reference in New Issue
Block a user