osm 0.1.17 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +45 -0
- data/Guardfile +9 -0
- data/README.md +15 -13
- data/lib/osm.rb +14 -0
- data/lib/osm/activity.rb +44 -13
- data/lib/osm/api.rb +48 -12
- data/lib/osm/api_access.rb +9 -6
- data/lib/osm/due_badges.rb +5 -4
- data/lib/osm/event.rb +87 -107
- data/lib/osm/flexi_record.rb +159 -118
- data/lib/osm/grouping.rb +41 -2
- data/lib/osm/{evening.rb → meeting.rb} +71 -64
- data/lib/osm/member.rb +88 -68
- data/lib/osm/model.rb +120 -30
- data/lib/osm/register.rb +23 -14
- data/lib/osm/section.rb +40 -100
- data/lib/osm/term.rb +35 -24
- data/osm.gemspec +2 -0
- data/spec/osm/activity_spec.rb +190 -173
- data/spec/osm/api_access_spec.rb +15 -7
- data/spec/osm/api_spec.rb +54 -27
- data/spec/osm/event_spec.rb +95 -84
- data/spec/osm/flexi_record_spec.rb +138 -35
- data/spec/osm/grouping_spec.rb +59 -1
- data/spec/osm/{evening_spec.rb → meeting_spec.rb} +53 -53
- data/spec/osm/member_spec.rb +151 -45
- data/spec/osm/model_spec.rb +28 -34
- data/spec/osm/register_spec.rb +1 -1
- data/spec/osm/section_spec.rb +49 -82
- data/spec/osm/term_spec.rb +23 -15
- data/spec/spec_helper.rb +25 -0
- data/version.rb +1 -1
- metadata +41 -18
data/lib/osm/model.rb
CHANGED
@@ -42,6 +42,27 @@ module Osm
|
|
42
42
|
id
|
43
43
|
end
|
44
44
|
|
45
|
+
# Get a list of attributes which have changed
|
46
|
+
# @return Array[String] the names of attributes which have changed
|
47
|
+
def changed_attributes
|
48
|
+
attributes.keys.select{ |k| attributes[k] != @original_attributes[k] }
|
49
|
+
end
|
50
|
+
|
51
|
+
# Reset the list of attributes which have changed
|
52
|
+
def reset_changed_attributes
|
53
|
+
@original_attributes = attributes
|
54
|
+
end
|
55
|
+
|
56
|
+
|
57
|
+
# Override initialize to set @orig_attributes
|
58
|
+
old_initialize = instance_method(:initialize)
|
59
|
+
define_method :initialize do |*args|
|
60
|
+
ret_val = old_initialize.bind(self).call(*args)
|
61
|
+
@original_attributes = attributes
|
62
|
+
return ret_val
|
63
|
+
end
|
64
|
+
|
65
|
+
|
45
66
|
private
|
46
67
|
# Wrap cache calls
|
47
68
|
def self.cache_read(api, key)
|
@@ -67,47 +88,116 @@ module Osm
|
|
67
88
|
end
|
68
89
|
|
69
90
|
|
70
|
-
#
|
71
|
-
# @param [Osm::Api] The api to use to make the request
|
72
|
-
# @param [Fixnum,
|
91
|
+
# Raise an exception if the user does not have access to a section
|
92
|
+
# @param [Osm::Api] api The api to use to make the request
|
93
|
+
# @param [Osm::Section, Fixnum, #to_i] section The Section (or its ID) the permission is required on
|
94
|
+
# @!macro options_get
|
95
|
+
# @raise [Osm::Forbidden] If the Api user can not access the section
|
96
|
+
def self.require_access_to_section(api, section, options={})
|
97
|
+
unless api.get_user_permissions(options).keys.include?(section.to_i)
|
98
|
+
raise Osm::Forbidden, "You do not have access to that section"
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
# Check if the user has access to a section
|
103
|
+
# @param [Osm::Api] api The api to use to make the request
|
104
|
+
# @param [Osm::Section, Fixnum, #to_i] section The Section (or its ID) the permission is required on
|
105
|
+
# @!macro options_get
|
106
|
+
def self.can_access_section?(api, section, options={})
|
107
|
+
api.get_user_permissions(options).keys.include?(section.to_i)
|
108
|
+
end
|
109
|
+
|
110
|
+
# Raise an exception if the user does not have the relevant permission
|
111
|
+
# @param [Osm::Api] api The api to use to make the request
|
112
|
+
# @param [Symbol] to What action is required to be done (e.g. :read or :write)
|
113
|
+
# @param [Symbol] on What the OSM permission is required on (e.g. :member or :programme)
|
114
|
+
# @param [Osm::Section, Fixnum, #to_i] section The Section (or its ID) the permission is required on
|
115
|
+
# @!macro options_get
|
116
|
+
# @raise [Osm::Forbidden] If the Api user does not have the required permission
|
117
|
+
def self.require_permission(api, to, on, section, options={})
|
118
|
+
section_id = section.to_i
|
119
|
+
|
120
|
+
# Check user's permissions in OSM
|
121
|
+
permissions = api.get_user_permissions(options)
|
122
|
+
permissions = permissions[section_id] || {}
|
123
|
+
permissions = permissions[on] || []
|
124
|
+
unless permissions.include?(to)
|
125
|
+
raise Osm::Forbidden, "Your OSM user does not have permission to #{to} on #{on} for #{Osm::Section.get(api, section_id, options).try(:name)}"
|
126
|
+
end
|
127
|
+
|
128
|
+
# Check what the user gave our API
|
129
|
+
permissions = Osm::ApiAccess.get_ours(api, section_id, options).permissions
|
130
|
+
permissions = permissions[on] || []
|
131
|
+
unless permissions.include?(to)
|
132
|
+
raise Osm::Forbidden, "You have not granted the #{to} permissions on #{on} to the #{api.api_name} API for #{Osm::Section.get(api, section_id, options).try(:name)}"
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
# Raise an exception if the user does not have the relevant permission
|
137
|
+
# @param [Osm::Api] api The api to use to make the request
|
138
|
+
# @param [Symbol] level The OSM subscription level required (e.g. :gold)
|
139
|
+
# @param [Osm::Section, Fixnum, #to_i] section The Section (or its ID) the subscription is required on
|
73
140
|
# @!macro options_get
|
74
|
-
# @
|
75
|
-
def self.
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
141
|
+
# @raise [Osm::Forbidden] If the Section does not have the required OSM Subscription (or higher)
|
142
|
+
def self.require_subscription(api, level, section, options={})
|
143
|
+
section = Osm::Section.get(api, section, options) if section.is_a?(Fixnum)
|
144
|
+
if level.is_a?(Symbol) # Convert to Fixnum
|
145
|
+
case level
|
146
|
+
when :bronze
|
147
|
+
level = 1
|
148
|
+
when :silver
|
149
|
+
level = 2
|
150
|
+
when :gold
|
151
|
+
level = 3
|
152
|
+
else
|
153
|
+
level = 0
|
154
|
+
end
|
155
|
+
end
|
156
|
+
if section.nil? || section.subscription_level < level
|
157
|
+
raise Osm::Forbidden, "Insufficent OSM subscription level (#{level} required for #{section.name})"
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
# Raise an exception if the user does not have the relevant permission
|
162
|
+
# @param [Osm::Api] api The api to use to make the request
|
163
|
+
# @param [Symbol] to What action is required to be done (e.g. :read or :write)
|
164
|
+
# @param [Symbol] on What the OSM permission is required on (e.g. :member or :programme)
|
165
|
+
# @param [Osm::Section, Fixnum, #to_i] section The Section (or its ID) the permission is required on
|
86
166
|
# @!macro options_get
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
167
|
+
def self.require_ability_to(api, to, on, section, options={})
|
168
|
+
require_permission(api, to, on, section, options)
|
169
|
+
require_subscription(api, :silver, section, options) if [:register, :contact, :events, :flexi].include?(on)
|
170
|
+
require_subscription(api, :gold, section, options) if [:finance].include?(on)
|
91
171
|
end
|
92
172
|
|
93
173
|
|
94
|
-
#
|
95
|
-
# @param [Osm::Api] The api to use to make the request
|
96
|
-
# @param [Fixnum
|
97
|
-
# @param [
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
174
|
+
# Get a list of items given a list of item IDs
|
175
|
+
# @param [Osm::Api] api The api to use to make the request
|
176
|
+
# @param [Array<Fixnum>] ids The ids of the items to get
|
177
|
+
# @param [String] key The key for getting an item from the cache (the key [key, id] is generated)
|
178
|
+
# @param [Array] argumentss The arguments to pass to get_all
|
179
|
+
# @!macro options_get
|
180
|
+
# @param [Symbol] get_all_method The method to get all items (either :get_all or :get_for_section)
|
181
|
+
# @return [Array] An array of the items
|
182
|
+
def self.get_from_ids(api, ids, key, arguments=[], options, get_all_method)
|
183
|
+
raise ArgumentError, "get_al_method is invalid" unless [:get_all, :get_for_section].include?(get_all_method)
|
184
|
+
items = Array.new
|
185
|
+
ids.each do |id|
|
186
|
+
if cache_exist?(api, [key, id])
|
187
|
+
items.push cache_read(api, [*key, id])
|
188
|
+
else
|
189
|
+
# At least this one item is not in the cache - we might as well refresh the lot
|
190
|
+
return self.send(get_all_method, api, *arguments, options.merge(:no_cache => true))
|
191
|
+
end
|
102
192
|
end
|
103
|
-
|
193
|
+
return items
|
104
194
|
end
|
105
195
|
|
106
196
|
|
107
197
|
# Make selected class methods instance methods too
|
108
198
|
%w{
|
109
|
-
cache_read cache_write cache_exist? cache_delete
|
110
|
-
|
199
|
+
cache_read cache_write cache_exist? cache_delete require_access_to_section
|
200
|
+
can_access_section? require_permission require_subscription require_ability_to
|
111
201
|
}.each do |method_name|
|
112
202
|
define_method method_name do |*options|
|
113
203
|
self.class.send(method_name, *options)
|
data/lib/osm/register.rb
CHANGED
@@ -4,16 +4,17 @@ module Osm
|
|
4
4
|
|
5
5
|
# Get register structure
|
6
6
|
# @param [Osm::Api] api The api to use to make the request
|
7
|
-
# @param [Osm::Section, Fixnum] section
|
8
|
-
# @param [Osm::Term, Fixnum, nil]
|
7
|
+
# @param [Osm::Section, Fixnum, #to_i] section The section (or its ID) to get the structure for
|
8
|
+
# @param [Osm::Term, Fixnum, #to_i, nil] term The term (or its ID) to get the structure for, passing nil causes the current term to be used
|
9
9
|
# @!macro options_get
|
10
10
|
# @return [Array<Osm::Register::Field>] representing the fields of the register
|
11
11
|
def self.get_structure(api, section, term=nil, options={})
|
12
|
+
Osm::Model.require_ability_to(api, :read, :register, section, options)
|
12
13
|
section_id = section.to_i
|
13
14
|
term_id = term.nil? ? Osm::Term.get_current_term_for_section(api, section).id : term.to_i
|
14
15
|
cache_key = ['register_structure', section_id, term_id]
|
15
16
|
|
16
|
-
if !options[:no_cache] && Osm::Model.cache_exist?(api, cache_key)
|
17
|
+
if !options[:no_cache] && Osm::Model.cache_exist?(api, cache_key)
|
17
18
|
return Osm::Model.cache_read(api, cache_key)
|
18
19
|
end
|
19
20
|
|
@@ -40,16 +41,17 @@ module Osm
|
|
40
41
|
|
41
42
|
# Get register attendance
|
42
43
|
# @param [Osm::Api] api The api to use to make the request
|
43
|
-
# @param [Osm::Section, Fixnum] section
|
44
|
-
# @param [Osm::Term, Fixnum, nil]
|
44
|
+
# @param [Osm::Section, Fixnum, #to_i] section The section (or its ID) to get the register for
|
45
|
+
# @param [Osm::Term, Fixnum, #to_i, nil] term The term (or its ID) to get the register for, passing nil causes the current term to be used
|
45
46
|
# @!macro options_get
|
46
47
|
# @return [Array<Register::Attendance>] representing the attendance of each member
|
47
48
|
def self.get_attendance(api, section, term=nil, options={})
|
49
|
+
Osm::Model.require_ability_to(api, :read, :register, section, options)
|
48
50
|
section_id = section.to_i
|
49
51
|
term_id = term.nil? ? Osm::Term.get_current_term_for_section(api, section).id : term.to_i
|
50
52
|
cache_key = ['register_attendance', section_id, term_id]
|
51
53
|
|
52
|
-
if !options[:no_cache] && Osm::Model.cache_exist?(api, cache_key)
|
54
|
+
if !options[:no_cache] && Osm::Model.cache_exist?(api, cache_key)
|
53
55
|
return Osm::Model.cache_read(api, cache_key)
|
54
56
|
end
|
55
57
|
|
@@ -83,19 +85,26 @@ module Osm
|
|
83
85
|
# @param [Hash] data
|
84
86
|
# @option data [Osm::Api] :api The api to use to make the request
|
85
87
|
# @option data [Osm::Section] :section the section to update the register for
|
86
|
-
# @option data [Osm::Term,
|
88
|
+
# @option data [Osm::Term, #to_i, nil] :term The term (or its ID) to get the register for, passing nil causes the current term to be used
|
87
89
|
# @option data [Osm::Evening, DateTime, Date] :evening the evening to update the register on
|
88
90
|
# @option data [String] :attendance what to mark the attendance as, one of "Yes", "No" or "Absent"
|
89
91
|
# @option data [Fixnum, Array<Fixnum>, Osm::Member, Array<Osm::Member>] :members the members (or their ids) to update
|
90
92
|
# @option data [Array<Hash>] :completed_badge_requirements (optional) the badge requirements to mark as completed, selected from the Hash returned by the get_badge_requirements_for_evening method
|
91
93
|
# @return [Boolean] whether the update succedded
|
94
|
+
# @raise [Osm::ArgumentIsInvalid] If data[:attendance] is not "Yes", "No" or "Absent"
|
95
|
+
# @raise [Osm::ArgumentIsInvalid] If data[:section] is missing
|
96
|
+
# @raise [Osm::ArgumentIsInvalid] If data[:evening] is missing
|
97
|
+
# @raise [Osm::ArgumentIsInvalid] If data[:members] is missing
|
98
|
+
# @raise [Osm::ArgumentIsInvalid] If data[:api] is missing
|
92
99
|
def self.update_attendance(data={})
|
93
|
-
raise ArgumentIsInvalid, ':attendance is invalid' unless ['Yes', 'No', 'Absent'].include?(data[:attendance])
|
94
|
-
raise ArgumentIsInvalid, ':section is missing' if data[:section].nil?
|
95
|
-
raise ArgumentIsInvalid, ':evening is missing' if data[:evening].nil?
|
96
|
-
raise ArgumentIsInvalid, ':members is missing' if data[:members].nil?
|
97
|
-
|
100
|
+
raise Osm::ArgumentIsInvalid, ':attendance is invalid' unless ['Yes', 'No', 'Absent'].include?(data[:attendance])
|
101
|
+
raise Osm::ArgumentIsInvalid, ':section is missing' if data[:section].nil?
|
102
|
+
raise Osm::ArgumentIsInvalid, ':evening is missing' if data[:evening].nil?
|
103
|
+
raise Osm::ArgumentIsInvalid, ':members is missing' if data[:members].nil?
|
104
|
+
raise Osm::ArgumentIsInvalid, ':api is missing' if data[:api].nil?
|
98
105
|
api = data[:api]
|
106
|
+
Osm::Model.require_ability_to(api, :write, :register, data[:section])
|
107
|
+
|
99
108
|
term_id = data[:term].nil? ? Osm::Term.get_current_term_for_section(api, section).id : data[:term].to_i
|
100
109
|
|
101
110
|
data[:members] = [*data[:members]].map{ |member| (member.is_a?(Fixnum) ? member : member.id).to_s } # Make sure it's an Array of Strings
|
@@ -136,7 +145,7 @@ module Osm
|
|
136
145
|
|
137
146
|
# @!method initialize
|
138
147
|
# Initialize a new RegisterField
|
139
|
-
# @param [Hash] attributes
|
148
|
+
# @param [Hash] attributes The hash of attributes (see attributes for descriptions, use Symbol of attribute name as the key)
|
140
149
|
|
141
150
|
end # Class Register::Field
|
142
151
|
|
@@ -178,7 +187,7 @@ module Osm
|
|
178
187
|
|
179
188
|
# @!method initialize
|
180
189
|
# Initialize a new registerData
|
181
|
-
# @param [Hash] attributes
|
190
|
+
# @param [Hash] attributes The hash of attributes (see attributes for descriptions, use Symbol of attribute name as the key)
|
182
191
|
|
183
192
|
end # Class Register::Data
|
184
193
|
|
data/lib/osm/section.rb
CHANGED
@@ -12,7 +12,7 @@ module Osm
|
|
12
12
|
# @!attribute [rw] group_name
|
13
13
|
# @return [String] the group name
|
14
14
|
# @!attribute [rw] subscription_level
|
15
|
-
# @return [
|
15
|
+
# @return [Fixnum] what subscription the section has to OSM (1-bronze, 2-silver, 3-gold)
|
16
16
|
# @!attribute [rw] subscription_expires
|
17
17
|
# @return [Date] when the section's subscription to OSM expires
|
18
18
|
# @!attribute [rw] type
|
@@ -66,7 +66,7 @@ module Osm
|
|
66
66
|
attribute :name, :type => String
|
67
67
|
attribute :group_id, :type => Integer
|
68
68
|
attribute :group_name, :type => String
|
69
|
-
attribute :subscription_level, :default =>
|
69
|
+
attribute :subscription_level, :default => 1
|
70
70
|
attribute :subscription_expires, :type => Date
|
71
71
|
attribute :type, :default => :unknown
|
72
72
|
attribute :column_names, :default => {}
|
@@ -120,7 +120,7 @@ module Osm
|
|
120
120
|
validates_presence_of :mobile_fields, :unless => Proc.new { |a| a.mobile_fields == {} }
|
121
121
|
validates_presence_of :flexi_records, :unless => Proc.new { |a| a.flexi_records == [] }
|
122
122
|
|
123
|
-
validates_inclusion_of :subscription_level, :in =>
|
123
|
+
validates_inclusion_of :subscription_level, :in => (1..3), :message => 'is not a valid subscription level'
|
124
124
|
validates_inclusion_of :gocardless, :in => [true, false]
|
125
125
|
validates_inclusion_of :myscout_events, :in => [true, false]
|
126
126
|
validates_inclusion_of :myscout_badges, :in => [true, false]
|
@@ -138,34 +138,32 @@ module Osm
|
|
138
138
|
|
139
139
|
# @!method initialize
|
140
140
|
# Initialize a new Section
|
141
|
-
# @param [Hash] attributes
|
141
|
+
# @param [Hash] attributes The hash of attributes (see attributes for descriptions, use Symbol of attribute name as the key)
|
142
142
|
|
143
143
|
|
144
144
|
# Get the user's sections
|
145
|
-
# @param [Osm::Api] The api to use to make the request
|
145
|
+
# @param [Osm::Api] api The api to use to make the request
|
146
146
|
# @!macro options_get
|
147
147
|
# @return [Array<Osm::Section>]
|
148
148
|
def self.get_all(api, options={})
|
149
149
|
cache_key = ['sections', api.user_id]
|
150
|
-
subscription_levels = {
|
151
|
-
1 => :bronze,
|
152
|
-
2 => :silver,
|
153
|
-
3 => :gold,
|
154
|
-
}
|
155
150
|
|
156
151
|
if !options[:no_cache] && cache_exist?(api, cache_key)
|
157
|
-
|
152
|
+
ids = cache_read(api, cache_key)
|
153
|
+
return get_from_ids(api, ids, 'section', options, :get_all)
|
158
154
|
end
|
159
155
|
|
160
156
|
data = api.perform_query('api.php?action=getUserRoles')
|
161
157
|
|
162
158
|
result = Array.new
|
159
|
+
ids = Array.new
|
163
160
|
permissions = Hash.new
|
164
161
|
data.each do |role_data|
|
165
162
|
unless role_data['section'].eql?('discount') # It's not an actual section
|
166
163
|
section_data = role_data['sectionConfig'].is_a?(String) ? ActiveSupport::JSON.decode(role_data['sectionConfig']) : role_data['sectionConfig']
|
167
164
|
myscout_data = section_data['portal'] || {}
|
168
165
|
section_data['portalExpires'] ||= {}
|
166
|
+
section_id = Osm::to_i_or_nil(role_data['sectionid'])
|
169
167
|
|
170
168
|
# Make sense of flexi records
|
171
169
|
fr_data = []
|
@@ -176,16 +174,17 @@ module Osm
|
|
176
174
|
# Expect item to be: {:name=>String, :extraid=>Fixnum}
|
177
175
|
# Sometimes get item as: [String, {"name"=>String, "extraid"=>Fixnum}]
|
178
176
|
record_data = record_data[1] if record_data.is_a?(Array)
|
179
|
-
flexi_records.push FlexiRecord.new(
|
177
|
+
flexi_records.push Osm::FlexiRecord.new(
|
180
178
|
:id => Osm::to_i_or_nil(record_data['extraid']),
|
181
179
|
:name => record_data['name'],
|
180
|
+
:section_id => section_id,
|
182
181
|
)
|
183
182
|
end
|
184
183
|
|
185
184
|
section = new(
|
186
|
-
:id =>
|
185
|
+
:id => section_id,
|
187
186
|
:name => role_data['sectionname'],
|
188
|
-
:subscription_level => (
|
187
|
+
:subscription_level => Osm::to_i_or_nil(section_data['subscription_level']),
|
189
188
|
:subscription_expires => Osm::parse_date(section_data['subscription_expires']),
|
190
189
|
:type => !section_data['sectionType'].nil? ? section_data['sectionType'].to_sym : (!section_data['section'].nil? ? section_data['section'].to_sym : :unknown),
|
191
190
|
:num_scouts => section_data['numscouts'],
|
@@ -216,27 +215,30 @@ module Osm
|
|
216
215
|
)
|
217
216
|
|
218
217
|
result.push section
|
218
|
+
ids.push section.id
|
219
219
|
cache_write(api, ['section', section.id], section)
|
220
|
-
permissions.merge!(section.id => make_permissions_hash(role_data['permissions']))
|
220
|
+
permissions.merge!(section.id => Osm.make_permissions_hash(role_data['permissions']))
|
221
221
|
end
|
222
222
|
end
|
223
223
|
|
224
|
-
|
225
|
-
|
224
|
+
permissions.each do |s_id, perms|
|
225
|
+
api.set_user_permissions(s_id, perms)
|
226
|
+
end
|
227
|
+
cache_write(api, cache_key, ids)
|
226
228
|
return result
|
227
229
|
end
|
228
230
|
|
229
231
|
|
230
232
|
# Get a section
|
231
|
-
# @param [Osm::Api] The api to use to make the request
|
232
|
-
# @param [Fixnum] section_id
|
233
|
+
# @param [Osm::Api] api The api to use to make the request
|
234
|
+
# @param [Fixnum] section_id The section id of the required section
|
233
235
|
# @!macro options_get
|
234
236
|
# @return nil if an error occured or the user does not have access to that section
|
235
237
|
# @return [Osm::Section]
|
236
238
|
def self.get(api, section_id, options={})
|
237
239
|
cache_key = ['section', section_id]
|
238
240
|
|
239
|
-
if !options[:no_cache] && cache_exist?(api, cache_key) &&
|
241
|
+
if !options[:no_cache] && cache_exist?(api, cache_key) && can_access_section?(api, section_id)
|
240
242
|
return cache_read(api, cache_key)
|
241
243
|
end
|
242
244
|
|
@@ -250,39 +252,15 @@ module Osm
|
|
250
252
|
end
|
251
253
|
|
252
254
|
|
253
|
-
# Get API user's permissions
|
254
|
-
# @param [Osm::Api] The api to use to make the request
|
255
|
-
# @!macro options_get
|
256
|
-
# @return nil if an error occured or the user does not have access to that section
|
257
|
-
# @return [Hash] {section_id => permissions_hash}
|
258
|
-
def self.fetch_user_permissions(api, options={})
|
259
|
-
cache_key = ['permissions', api.user_id]
|
260
|
-
|
261
|
-
if !options[:no_cache] && cache_exist?(api, cache_key)
|
262
|
-
return cache_read(api, cache_key)
|
263
|
-
end
|
264
|
-
|
265
|
-
data = api.perform_query('api.php?action=getUserRoles')
|
266
|
-
|
267
|
-
all_permissions = Hash.new
|
268
|
-
data.each do |item|
|
269
|
-
unless item['section'].eql?('discount') # It's not an actual section
|
270
|
-
all_permissions.merge!(Osm::to_i_or_nil(item['sectionid']) => make_permissions_hash(item['permissions']))
|
271
|
-
end
|
272
|
-
end
|
273
|
-
cache_write(api, cache_key, all_permissions)
|
274
|
-
return all_permissions
|
275
|
-
end
|
276
|
-
|
277
|
-
|
278
255
|
# Get the section's notepad from OSM
|
279
|
-
# @param [Osm::Api] The api to use to make the request
|
256
|
+
# @param [Osm::Api] api The api to use to make the request
|
280
257
|
# @!macro options_get
|
281
258
|
# @return [String] the section's notepad
|
282
259
|
def get_notepad(api, options={})
|
260
|
+
require_access_to_section(api, self, options)
|
283
261
|
cache_key = ['notepad', id]
|
284
262
|
|
285
|
-
if !options[:no_cache] && cache_exist?(api, cache_key) &&
|
263
|
+
if !options[:no_cache] && cache_exist?(api, cache_key) && can_access_section?(api, section_id)
|
286
264
|
return cache_read(api, cache_key)
|
287
265
|
end
|
288
266
|
|
@@ -303,6 +281,7 @@ module Osm
|
|
303
281
|
# @param [String] content The content of the notepad
|
304
282
|
# @return [Boolean] whether the notepad was sucessfully updated
|
305
283
|
def set_notepad(api, content)
|
284
|
+
require_access_to_section(api, self)
|
306
285
|
data = api.perform_query("users.php?action=updateNotepad§ionid=#{id}", {'value' => content})
|
307
286
|
|
308
287
|
if data.is_a?(Hash) && data['ok'] # Success
|
@@ -314,15 +293,16 @@ module Osm
|
|
314
293
|
|
315
294
|
|
316
295
|
# Get badge stock levels
|
317
|
-
# @param [Osm::Api] The api to use to make the request
|
318
|
-
# @param [Osm::Term, Fixnum, nil]
|
296
|
+
# @param [Osm::Api] api The api to use to make the request
|
297
|
+
# @param [Osm::Term, Fixnum, #to_i, nil] term The term (or its ID) to get the stock levels for, passing nil causes the current term to be used
|
319
298
|
# @!macro options_get
|
320
299
|
# @return Hash
|
321
300
|
def get_badge_stock(api, term=nil, options={})
|
301
|
+
require_ability_to(api, :read, :badge, self, options)
|
322
302
|
term_id = term.nil? ? Osm::Term.get_current_term_for_section(api, self).id : term.to_i
|
323
303
|
cache_key = ['badge_stock', id, term_id]
|
324
304
|
|
325
|
-
if !options[:no_cache] && cache_exist?(api, cache_key)
|
305
|
+
if !options[:no_cache] && cache_exist?(api, cache_key)
|
326
306
|
return cache_read(api, cache_key)
|
327
307
|
end
|
328
308
|
|
@@ -366,6 +346,16 @@ module Osm
|
|
366
346
|
end
|
367
347
|
end
|
368
348
|
|
349
|
+
# Get the name for the section's subscription level
|
350
|
+
# @return [String, nil] the name of the subscription level (nil if no name exists)
|
351
|
+
def subscription_level_name
|
352
|
+
return {
|
353
|
+
1 => 'Bronze',
|
354
|
+
2 => 'Silver',
|
355
|
+
3 => 'Gold',
|
356
|
+
}[subscription_level]
|
357
|
+
end
|
358
|
+
|
369
359
|
def <=>(another)
|
370
360
|
begin
|
371
361
|
compare_group_name = group_name <=> another.group_name
|
@@ -389,56 +379,6 @@ module Osm
|
|
389
379
|
end
|
390
380
|
end
|
391
381
|
|
392
|
-
|
393
|
-
private
|
394
|
-
def self.make_permissions_hash(permissions)
|
395
|
-
return {} unless permissions.is_a?(Hash)
|
396
|
-
|
397
|
-
permissions_map = {
|
398
|
-
10 => [:read],
|
399
|
-
20 => [:read, :write],
|
400
|
-
100 => [:read, :write, :administer],
|
401
|
-
}
|
402
|
-
|
403
|
-
return permissions.inject({}) do |new_hash, (key, value)|
|
404
|
-
new_hash[key.to_sym] = (permissions_map[value.to_i] || [])
|
405
|
-
new_hash
|
406
|
-
end
|
407
|
-
end
|
408
|
-
|
409
|
-
|
410
|
-
class FlexiRecord
|
411
|
-
include ::ActiveAttr::MassAssignmentSecurity
|
412
|
-
include ::ActiveAttr::Model
|
413
|
-
|
414
|
-
# @!attribute [rw] id
|
415
|
-
# @return [Fixnum] the aid of the flexi-record
|
416
|
-
# @!attribute [rw] name
|
417
|
-
# @return [String] the name given to the flexi-record
|
418
|
-
|
419
|
-
attribute :id, :type => Integer
|
420
|
-
attribute :name, :type => String
|
421
|
-
|
422
|
-
attr_accessible :id, :name
|
423
|
-
|
424
|
-
validates_numericality_of :id, :only_integer=>true, :greater_than=>0
|
425
|
-
validates_presence_of :name
|
426
|
-
|
427
|
-
|
428
|
-
# @!method initialize
|
429
|
-
# Initialize a new FlexiRecord
|
430
|
-
# @param [Hash] attributes the hash of attributes (see attributes for descriptions, use Symbol of attribute name as the key)
|
431
|
-
|
432
|
-
def <=>(another)
|
433
|
-
begin
|
434
|
-
return self.name <=> another.name
|
435
|
-
rescue NoMethodError
|
436
|
-
return 1
|
437
|
-
end
|
438
|
-
end
|
439
|
-
|
440
|
-
end # Class Section::FlexiRecord
|
441
|
-
|
442
382
|
end # Class Section
|
443
383
|
|
444
384
|
end # Module
|