Class: UserAccessProvider
- Inherits:
-
Object
- Object
- UserAccessProvider
- Defined in:
- lib/models/user_access_provider.rb
Instance Method Summary collapse
- #authenticate(token) ⇒ Object
- #find_user(name) ⇒ Object
-
#initialize(db_wrapper) ⇒ UserAccessProvider
constructor
A new instance of UserAccessProvider.
- #is_admin? ⇒ Boolean
- #is_allowed?(action, collection_name) ⇒ Boolean
- #is_allowed_in_console? ⇒ Boolean
- #is_allowed_in_explorer? ⇒ Boolean
- #is_public_read?(collection_name) ⇒ Boolean
- #log_out ⇒ Object
- #logged_in? ⇒ Boolean
Constructor Details
#initialize(db_wrapper) ⇒ UserAccessProvider
Returns a new instance of UserAccessProvider.
3 4 5 |
# File 'lib/models/user_access_provider.rb', line 3 def initialize(db_wrapper) @db_wrapper = db_wrapper end |
Instance Method Details
#authenticate(token) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/models/user_access_provider.rb', line 46 def authenticate(token) response = authenticate_with_janrain token if(response["stat"] == "ok") id = response["profile"]["googleUserId"] name = response["profile"]["displayName"] email = response["profile"]["email"] ref = name.gsub(/\s+/, "-").downcase existing_user = find_user ref if(existing_user) UserContext.current_user = existing_user else sign_up_user ref, id, name, email end return true end return false end |
#find_user(name) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/models/user_access_provider.rb', line 38 def find_user(name) user = @db_wrapper.collection('users').find_document(name) if(user) return user.standardised_document.to_obj end nil end |
#is_admin? ⇒ Boolean
68 69 70 |
# File 'lib/models/user_access_provider.rb', line 68 def is_admin? UserContext.current_user.isadmin end |
#is_allowed?(action, collection_name) ⇒ Boolean
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/models/user_access_provider.rb', line 7 def is_allowed?(action, collection_name) return false if !logged_in? access_control = find_access_control_for_collection(collection_name) return true if is_admin? return true if access_control == nil if(action == :read) read_users = access_control.read_users return true if is_public_read? collection_name return true if !read_users return true if read_users.include? UserContext.current_user.alias elsif(action == :write) write_users = access_control.write_users return true if !write_users return true if write_users == "*" return true if write_users.include? UserContext.current_user.alias elsif(action == :modify) modify_users = access_control.modify_users return true if modify_users == "*" return true if !modify_users return true if modify_users.include? UserContext.current_user.alias end false end |
#is_allowed_in_console? ⇒ Boolean
72 73 74 |
# File 'lib/models/user_access_provider.rb', line 72 def is_allowed_in_console? UserContext.current_user.isadmin end |
#is_allowed_in_explorer? ⇒ Boolean
76 77 78 |
# File 'lib/models/user_access_provider.rb', line 76 def is_allowed_in_explorer? UserContext.current_user.isallowed end |
#is_public_read?(collection_name) ⇒ Boolean
32 33 34 35 36 |
# File 'lib/models/user_access_provider.rb', line 32 def is_public_read?(collection_name) access_control = find_access_control_for_collection(collection_name) return true if access_control == nil return true if access_control.read_users == "*" end |
#log_out ⇒ Object
84 85 86 |
# File 'lib/models/user_access_provider.rb', line 84 def log_out UserContext.current_user = nil end |
#logged_in? ⇒ Boolean
80 81 82 |
# File 'lib/models/user_access_provider.rb', line 80 def logged_in? UserContext.current_user != nil end |