jungle_path 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile +1 -0
- data/jungle_path.gemspec +4 -1
- data/lib/jungle_path/api/helpers/auth.rb +6 -10
- data/lib/jungle_path/api/helpers/defaults.rb +7 -6
- data/lib/jungle_path/api/helpers/logging.rb +7 -1
- data/lib/jungle_path/api/helpers.rb +2 -2
- data/lib/jungle_path/api/template.erb +2 -2
- data/lib/jungle_path/app/api/{server_base.rb → base.rb} +24 -23
- data/lib/jungle_path/app/api/{server_custom.rb → custom.rb} +15 -16
- data/lib/jungle_path/app/api/{server_gen.rb → generated.rb} +2 -2
- data/lib/jungle_path/app/auth/authorization.rb +15 -14
- data/lib/jungle_path/app/config/config.rb +8 -9
- data/lib/jungle_path/app/config/override.rb +2 -1
- data/lib/jungle_path/app/config.ru +6 -5
- data/lib/jungle_path/app/controllers/controller.rb +197 -0
- data/lib/jungle_path/app/controllers/generated.rb +13 -0
- data/lib/jungle_path/app/db/db.rb +13 -0
- data/lib/jungle_path/app/schemas/schema.rb +81 -0
- data/lib/jungle_path/app/services/email.rb +138 -0
- data/lib/jungle_path/app/services/sms.rb +17 -0
- data/lib/jungle_path/app/web_apps/public/index.html +10 -0
- data/lib/jungle_path/app/ztools/db/migrations/000_root.rb +1 -0
- data/lib/jungle_path/app/ztools/zbootstrapdata.rb +57 -0
- data/lib/jungle_path/app/ztools/zcreatedb.rb +19 -0
- data/lib/jungle_path/app/ztools/zgen.rb +9 -0
- data/lib/jungle_path/app/ztools/zgen_node_tree.rb +14 -0
- data/lib/jungle_path/app/ztools/zmigrate.rb +7 -0
- data/lib/jungle_path/app/ztools/zport_data.rb +292 -0
- data/lib/jungle_path/authentication/auth_provider/default.rb +36 -29
- data/lib/jungle_path/authentication/data_provider/default.rb +55 -109
- data/lib/jungle_path/authentication/data_provider/test.rb +149 -0
- data/lib/jungle_path/authentication/data_provider.rb +1 -0
- data/lib/jungle_path/authentication/identity.rb +4 -4
- data/lib/jungle_path/authentication/password_hash.rb +8 -8
- data/lib/jungle_path/authorization/filter.rb +0 -3
- data/lib/jungle_path/config.rb +1 -1
- data/lib/jungle_path/controller/template.erb +2 -2
- data/lib/jungle_path/db_access.rb +6 -0
- data/lib/jungle_path/file/file.rb +21 -0
- data/lib/jungle_path/file.rb +3 -0
- data/lib/jungle_path/gen/api.rb +3 -3
- data/lib/jungle_path/gen/controllers.rb +20 -0
- data/lib/jungle_path/gen/db.rb +77 -0
- data/lib/jungle_path/gen/schema.rb +2 -2
- data/lib/jungle_path/gen.rb +1 -1
- data/lib/jungle_path/migration/migration.rb +31 -0
- data/lib/jungle_path/migration.rb +3 -0
- data/lib/jungle_path/rack/json_body_parser.rb +2 -2
- data/lib/jungle_path/schema/auth.rb +32 -40
- data/lib/jungle_path/sql/key.rb +0 -22
- data/lib/jungle_path/sql/query_filter.rb +2 -2
- data/lib/jungle_path/sql/role.rb +2 -2
- data/lib/jungle_path/sql/user.rb +21 -2
- data/lib/jungle_path/time/time.rb +9 -0
- data/lib/jungle_path/time.rb +3 -0
- data/lib/jungle_path/version.rb +3 -0
- data/lib/jungle_path.rb +4 -0
- metadata +28 -6
- data/lib/jungle_path/gen/controller.rb +0 -0
@@ -0,0 +1,292 @@
|
|
1
|
+
# (data) zportdata_from_sqlserver.rb
|
2
|
+
# select / transform / insert current GB data into new db.
|
3
|
+
#require 'pry-byebug'
|
4
|
+
require 'date'
|
5
|
+
|
6
|
+
require 'jungle_path/gen/db'
|
7
|
+
require 'jungle_path/gen/schema'
|
8
|
+
require 'jungle_path/db_model'
|
9
|
+
require 'jungle_path/sql/helpers'
|
10
|
+
require 'jungle_path/authentication/helpers'
|
11
|
+
require 'jungle_path/db_access/io/db'
|
12
|
+
require 'jungle_path/db_access/import/db_dir'
|
13
|
+
require 'jungle_path/logging'
|
14
|
+
|
15
|
+
require_relative '../schemas/schema'
|
16
|
+
require_relative '../controllers/controller'
|
17
|
+
require_relative '../db/db'
|
18
|
+
require_relative '../config/config'
|
19
|
+
require_relative 'zcreatedb'
|
20
|
+
require_relative 'zbootstrapdata'
|
21
|
+
|
22
|
+
# notes: key_id lookup has been hardcoded to use @current_key.
|
23
|
+
# users will not be ported, but must be added as new users in new system.
|
24
|
+
|
25
|
+
class Port
|
26
|
+
def initialize(source_db, target_db, target_db_postgresql, logger=nil, source_database_type=:sql_server)
|
27
|
+
@user_params = {id: 0, user_name: "root"}
|
28
|
+
@current_user = Schema::User.new(@user_params)
|
29
|
+
@current_key = @current_user
|
30
|
+
@logger = logger
|
31
|
+
log "initialize"
|
32
|
+
if source_database_type == :sql_server
|
33
|
+
# sql server:
|
34
|
+
@sql_true = "1"
|
35
|
+
@sql_allow_order_bys_in_sub_select = "top 1000000000"
|
36
|
+
@left_bracket = "["
|
37
|
+
@right_bracket = "]"
|
38
|
+
@convert_nvarchar_beg = "convert(nvarchar, "
|
39
|
+
@convert_nvarchar_end = ")"
|
40
|
+
else
|
41
|
+
# postgresql:
|
42
|
+
@sql_true = "true"
|
43
|
+
@sql_allow_order_bys_in_sub_select = ""
|
44
|
+
@left_bracket = ""
|
45
|
+
@right_bracket = ""
|
46
|
+
@convert_nvarchar_beg = ""
|
47
|
+
@convert_nvarchar_end = "::text"
|
48
|
+
end
|
49
|
+
|
50
|
+
@source_db = source_db
|
51
|
+
@target_db = target_db
|
52
|
+
@target_db_postgresql = target_db_postgresql
|
53
|
+
@lookup = Lookup.new(@target_db_postgresql)
|
54
|
+
|
55
|
+
if @target_db.respond_to? :log_path
|
56
|
+
Dir.mkdir @target_db.log_path unless Dir.exist? @target_db.log_path
|
57
|
+
@error_log_file_name = "#{@target_db.log_path}/error_#{DateTime.now.strftime("%Y%m%d_%H%M%S")}.log"
|
58
|
+
else
|
59
|
+
Dir.mkdir "../logs" unless Dir.exist? "../logs"
|
60
|
+
@error_log_file_name = "../logs/error_#{DateTime.now.strftime("%Y%m%d_%H%M%S")}.log"
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def reset_db
|
65
|
+
log 'reset_db'
|
66
|
+
JunglePath::Gen::DB.reset!(@target_db.postgresql)
|
67
|
+
end
|
68
|
+
|
69
|
+
def create_log_table
|
70
|
+
drop_table Controller::Log
|
71
|
+
create_table Controller::Log
|
72
|
+
@log_id = -1
|
73
|
+
end
|
74
|
+
|
75
|
+
def set_log_id
|
76
|
+
log_id = @target_db_postgresql.get_max_id_for_table(:log)
|
77
|
+
log_id = -1 if log_id == nil
|
78
|
+
@log_id = log_id
|
79
|
+
end
|
80
|
+
|
81
|
+
def create_db_and_schema
|
82
|
+
log 'create_db_and_schema'
|
83
|
+
ZCreateDb.run @target_db.postgresql
|
84
|
+
end
|
85
|
+
|
86
|
+
def initial_data
|
87
|
+
log 'initial_data', @target_db_postgresql
|
88
|
+
reset_table Controller::User
|
89
|
+
reset_table Controller::Role
|
90
|
+
reset_table Controller::UserRole
|
91
|
+
ZBootstrap.root_data(@target_db.postgresql)
|
92
|
+
end
|
93
|
+
|
94
|
+
def user
|
95
|
+
log 'user', @target_db_postgresql
|
96
|
+
drop_table Controller::Contact
|
97
|
+
create_table Controller::Contact
|
98
|
+
sql = JunglePath::SQL::Helpers.sql("
|
99
|
+
select
|
100
|
+
a.ROW_ID as siebel_id,
|
101
|
+
b.FST_NAME as first_name,
|
102
|
+
b.LAST_NAME as last_name,
|
103
|
+
lower(b.EMAIL_ADDR) as email,
|
104
|
+
b.CELL_PH_NUM as phone,
|
105
|
+
lower(a.LOGIN) as user_name,
|
106
|
+
a.PASSWORD as password,
|
107
|
+
a.CREATED as created_at,
|
108
|
+
a.CREATED_BY as created_by_siebel_id,
|
109
|
+
a.LAST_UPD as updated_at,
|
110
|
+
a.LAST_UPD_BY as updated_by_siebel_id
|
111
|
+
from dbo.S_USER a
|
112
|
+
join dbo.S_CONTACT b on a.ROW_ID = b.ROW_ID
|
113
|
+
order by a.ROW_ID
|
114
|
+
")
|
115
|
+
ds = @source_db.base[sql]
|
116
|
+
results = ds.all
|
117
|
+
id = @target_db_postgresql.get_max_id_for_table(:user)
|
118
|
+
results.each do |item|
|
119
|
+
id += 1
|
120
|
+
item[:user_id] = id
|
121
|
+
item[:contact_id] = id
|
122
|
+
end
|
123
|
+
count = 0
|
124
|
+
@target_db.transaction do
|
125
|
+
results.each do |h|
|
126
|
+
h = hash_values_nil_if_blank(h)
|
127
|
+
count += 1
|
128
|
+
puts "user: #{count} of #{results.length}."
|
129
|
+
h[:email] = nil unless h[:email] and h[:email].include?('@')
|
130
|
+
h[:password] = JunglePath::Authentication::Helpers.generate_api_key('generated_password_') #generate a random password for ported users. They will not have their password unless it is changed, so they will not be able to log on directly.
|
131
|
+
#also, defaulting portal users to active for now...:
|
132
|
+
hash = {id: h[:user_id], siebel_id: h[:siebel_id], user_name: h[:user_name], email: h[:email], first_name: h[:first_name], last_name: h[:last_name], password: h[:password], active: true}
|
133
|
+
user = Controller::User.new(@current_user, @current_key, hash, @target_db).insert
|
134
|
+
h[:password] = nil
|
135
|
+
Controller::SiebelUser.new(@current_user, @current_key, h, @target_db).insert
|
136
|
+
Controller::Contact.new(@current_user, @current_key, h, @target_db).insert
|
137
|
+
Controller::SiebelContact.new(@current_user, @current_key, h, @target_db).insert
|
138
|
+
end
|
139
|
+
end
|
140
|
+
@target_db_postgresql.reset_sequence_for_table(:user)
|
141
|
+
log "user: processed #{results.length} rows.", @target_db_postgresql
|
142
|
+
end
|
143
|
+
|
144
|
+
def log_error(msg, db=nil)
|
145
|
+
puts "log error: #{msg}"
|
146
|
+
@logger.error(msg)
|
147
|
+
db_log(msg, 'error', db) if db
|
148
|
+
end
|
149
|
+
|
150
|
+
def log_warn(msg, db=nil)
|
151
|
+
puts "log warn: #{msg}"
|
152
|
+
@logger.warn(msg)
|
153
|
+
db_log(msg, 'warn', db) if db
|
154
|
+
end
|
155
|
+
|
156
|
+
def log(msg, db=nil)
|
157
|
+
puts "log info: #{msg}"
|
158
|
+
@logger.info(msg)
|
159
|
+
db_log(msg, 'info', db) if db
|
160
|
+
end
|
161
|
+
|
162
|
+
def db_log(msg, type, db)
|
163
|
+
@log_id += 1
|
164
|
+
Controller::Log.new(@current_user, @current_key, {id: @log_id, name: "zport_data", type: type, item: msg, timestamp: Time.now.utc}, db).insert if db
|
165
|
+
end
|
166
|
+
|
167
|
+
private
|
168
|
+
|
169
|
+
def correlated_lookup table, value, search_column, target_column
|
170
|
+
@lookup.value(table, value, search_column, target_column)
|
171
|
+
end
|
172
|
+
|
173
|
+
def hash_values_nil_if_blank(hash)
|
174
|
+
hash.each do |key, value|
|
175
|
+
#puts "hash[#{key}] = '#{value}'" if value and value.to_s[0] == "\t"
|
176
|
+
hash[key] = nil_if_blank(value)
|
177
|
+
end
|
178
|
+
hash
|
179
|
+
end
|
180
|
+
|
181
|
+
def nil_if_blank(value)
|
182
|
+
if value.class == String
|
183
|
+
v = value.strip
|
184
|
+
v = nil if v.empty? or v == 'No Match Row Id'
|
185
|
+
v
|
186
|
+
else
|
187
|
+
value
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
191
|
+
def drop_table(controller_class)
|
192
|
+
controller_class.new(@current_user, @current_key, {}, @target_db).drop_table
|
193
|
+
end
|
194
|
+
|
195
|
+
def create_table(controller_class)
|
196
|
+
controller_class.new(@current_user, @current_key, {}, @target_db).create_table
|
197
|
+
end
|
198
|
+
|
199
|
+
def reset_table(controller_class)
|
200
|
+
drop_table controller_class
|
201
|
+
create_table controller_class
|
202
|
+
end
|
203
|
+
end
|
204
|
+
|
205
|
+
class Lookup
|
206
|
+
def initialize(db)
|
207
|
+
@target_db = db
|
208
|
+
@current_user = nil
|
209
|
+
@current_key = nil
|
210
|
+
@lookups = {}
|
211
|
+
end
|
212
|
+
|
213
|
+
def value table, value, search_column, target_column
|
214
|
+
key = "#{table}.#{search_column}"
|
215
|
+
@lookups[key] = load_lookup(table, search_column, target_column) unless @lookups[key]
|
216
|
+
@lookups[key][value]
|
217
|
+
end
|
218
|
+
|
219
|
+
private
|
220
|
+
def load_lookup table, search_column, target_column
|
221
|
+
lookup = {}
|
222
|
+
lookup[nil] = nil
|
223
|
+
lookup['No Match Row Id'] = nil
|
224
|
+
sql = SQLHelpers.sql("select #{search_column}, #{target_column} from #{table}")
|
225
|
+
ds = @target_db.base[sql]
|
226
|
+
results = ds.all
|
227
|
+
results.each do |item|
|
228
|
+
lookup[item[search_column]] = item[target_column]
|
229
|
+
end
|
230
|
+
lookup
|
231
|
+
end
|
232
|
+
end
|
233
|
+
|
234
|
+
def run_import port, db_log_target
|
235
|
+
skip_some_items = false
|
236
|
+
|
237
|
+
log = []
|
238
|
+
|
239
|
+
port.log 'run_import'
|
240
|
+
log << 'run_import'
|
241
|
+
|
242
|
+
unless skip_some_items
|
243
|
+
port.reset_db
|
244
|
+
log << 'reset_db'
|
245
|
+
end
|
246
|
+
|
247
|
+
port.create_log_table
|
248
|
+
log << 'create_log_table'
|
249
|
+
|
250
|
+
##port.create_db_and_schema
|
251
|
+
##log << 'create_db_and_schema'
|
252
|
+
|
253
|
+
port.set_log_id
|
254
|
+
log << 'set_log_id'
|
255
|
+
|
256
|
+
log.each do |item|
|
257
|
+
port.log item, db_log_target
|
258
|
+
end
|
259
|
+
|
260
|
+
unless skip_some_items
|
261
|
+
port.initial_data
|
262
|
+
port.user
|
263
|
+
end
|
264
|
+
rescue Exception => ex
|
265
|
+
port.log_error ex
|
266
|
+
raise
|
267
|
+
end
|
268
|
+
|
269
|
+
def get_logger
|
270
|
+
this_dir = File.expand_path(File.dirname(__FILE__))
|
271
|
+
one_up = File.expand_path('..', this_dir)
|
272
|
+
puts "one_up: #{one_up}."
|
273
|
+
logger = JunglePath::Logging.make_logger(one_up, 'zport_data.log', reset: true)
|
274
|
+
end
|
275
|
+
|
276
|
+
def run
|
277
|
+
logger = get_logger
|
278
|
+
logger.info('test!!!!!!!!!!!!!!!!!!!!!!11')
|
279
|
+
#binding.pry
|
280
|
+
# get source and target databases:
|
281
|
+
db_source = JunglePath::DBAccess::IO::DB.new(configatron.source.db, logger)
|
282
|
+
db_target = JunglePath::DBAccess::Import::DBDir.new(configatron.source.port_to.data_files_import, logger)
|
283
|
+
db_target_postgresql = JunglePath::DBAccess::IO::DB.new(db_target.postgresql, logger)
|
284
|
+
|
285
|
+
# setup and run data port:
|
286
|
+
port = Port.new(db_source, db_target, db_target_postgresql, logger) # (source_db, target_db)
|
287
|
+
run_import port, db_target_postgresql
|
288
|
+
|
289
|
+
## generate any tables in schema that do not already exist:
|
290
|
+
# Gen::Schema.create(Schema::Base.models_dependent_order, db_target.postgresql)
|
291
|
+
end
|
292
|
+
run
|
@@ -2,15 +2,15 @@ module JunglePath
|
|
2
2
|
module Authentication
|
3
3
|
module AuthProvider
|
4
4
|
class Default
|
5
|
-
def authenticate request, data_provider
|
6
|
-
puts "
|
7
|
-
data_provider = JunglePath::Authentication::DataProvider::Default.new unless data_provider
|
5
|
+
def authenticate request, data_provider, no_cache=false
|
6
|
+
puts "JunglePath::Authentication::AuthProvider::Default.authenticate"
|
7
|
+
#data_provider = JunglePath::Authentication::DataProvider::Default.new unless data_provider
|
8
8
|
remote_user = request.env['REMOTE_USER']
|
9
9
|
remote_password = request.env['REMOTE_PASSWORD']
|
10
10
|
puts "remote_user: #{remote_user}."
|
11
11
|
puts "remote_password: #{remote_password}."
|
12
12
|
identity = basic_authentication(data_provider, remote_user, remote_password, no_cache)
|
13
|
-
identity = basic_authentication(data_provider, remote_user, remote_password, true) unless identity and identity.valid?
|
13
|
+
#identity = basic_authentication(data_provider, remote_user, remote_password, true) unless identity and identity.valid?
|
14
14
|
end
|
15
15
|
|
16
16
|
def basic_authentication data_provider, remote_user, remote_password, no_cache=false
|
@@ -19,11 +19,13 @@ module JunglePath
|
|
19
19
|
puts "assume_identity: #{assume_identity}"
|
20
20
|
valid = false
|
21
21
|
identity = authenticate_identity(data_provider, identity, no_cache)
|
22
|
+
puts "authenticated identity: #{identity}."
|
22
23
|
identity = authorize_identity(data_provider, identity, no_cache)
|
23
|
-
|
24
|
-
assume_identity =
|
25
|
-
|
26
|
-
|
24
|
+
puts "authorized identity: #{identity}."
|
25
|
+
#assume_identity = authenticate_identity(assume_identity, no_cache) if assume_identity and identity and identity.valid?
|
26
|
+
#assume_identity = authorize_identity(data_provider, assume_identity, no_cache) if assume_identity
|
27
|
+
#valid = (assume_identity and assume_identity.valid?) or (identity and identity.valid?)
|
28
|
+
#return assume_identity if assume_identity
|
27
29
|
identity
|
28
30
|
end
|
29
31
|
|
@@ -44,39 +46,44 @@ module JunglePath
|
|
44
46
|
end
|
45
47
|
return identity, assume_identity
|
46
48
|
end
|
47
|
-
|
49
|
+
|
48
50
|
def authenticate_identity data_provider, identity, no_cache=false
|
49
|
-
|
51
|
+
idn = nil
|
50
52
|
if identity
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
53
|
+
idn = identity.dup
|
54
|
+
if user_name_is_key? identity.user_name, identity.remote_password
|
55
|
+
idn.user = data_provider.get_user_by_key(identity.user_name, no_cache, identity.remote_password)
|
56
|
+
idn.key = identity.user_name
|
57
|
+
else
|
58
|
+
idn.user = data_provider.get_user(identity.user_name, identity.remote_password, no_cache)
|
59
|
+
idn.key = nil
|
60
|
+
end
|
61
|
+
idn.valid = (idn.user and idn.user.is_valid)
|
55
62
|
end
|
56
|
-
|
63
|
+
idn
|
57
64
|
end
|
58
65
|
|
59
66
|
def authorize_identity data_provider, identity, no_cache
|
60
|
-
|
67
|
+
idn = nil
|
61
68
|
if identity
|
62
|
-
|
63
|
-
if
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
id.user.auth = id.auth if id.user
|
68
|
-
id.query_filters = data_provider.get_query_filters(id, no_cache)
|
69
|
+
idn = identity.dup
|
70
|
+
if idn.valid?
|
71
|
+
idn.role = data_provider.get_role(idn, no_cache)
|
72
|
+
idn.authorization_filter = data_provider.get_authorization_filter(idn, no_cache)
|
73
|
+
idn.query_filters = data_provider.get_query_filters(idn, no_cache)
|
69
74
|
else
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
id.user.auth = nil if id.user
|
74
|
-
id.query_filters = nil
|
75
|
+
idn.role = nil
|
76
|
+
idn.authorization_filter = nil
|
77
|
+
idn.query_filters = nil
|
75
78
|
end
|
76
79
|
end
|
77
|
-
|
80
|
+
idn
|
78
81
|
end
|
79
82
|
|
83
|
+
def user_name_is_key? user_name, password
|
84
|
+
#puts "user_name: #{user_name}, password: #{password}. password == nil: #{password == nil}."
|
85
|
+
user_name and user_name.start_with?("sk_") and !user_name.include?("@") and (password == nil or password.strip.length == 0)
|
86
|
+
end
|
80
87
|
end
|
81
88
|
end
|
82
89
|
end
|
@@ -1,139 +1,85 @@
|
|
1
1
|
module JunglePath
|
2
|
+
require 'jungle_path/authentication/password_hash'
|
2
3
|
require 'jungle_path/authorization/filter'
|
4
|
+
require 'jungle_path/schema/auth'
|
5
|
+
require 'jungle_path/sql/user'
|
3
6
|
module Authentication
|
4
7
|
module DataProvider
|
5
8
|
class Default
|
6
|
-
def initialize
|
7
|
-
@
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
restrictions: []
|
14
|
-
},
|
15
|
-
admin: {
|
16
|
-
id: 1,
|
17
|
-
name: :admin,
|
18
|
-
description: 'admin and add, edit and delete users, but not root users.',
|
19
|
-
permissions: [:admin],
|
20
|
-
restrictions: []
|
21
|
-
},
|
22
|
-
user: {
|
23
|
-
id: 2,
|
24
|
-
name: :user,
|
25
|
-
description: 'basic system user -- has read only access.',
|
26
|
-
permissions: [:read],
|
27
|
-
restrictions: [:query_only, :me_related]
|
28
|
-
}
|
29
|
-
}
|
30
|
-
@users = {
|
31
|
-
root: {
|
32
|
-
id: 0,
|
33
|
-
name: 'root',
|
34
|
-
email: nil,
|
35
|
-
phone: nil,
|
36
|
-
active: true,
|
37
|
-
user_name: :root,
|
38
|
-
password: 'test',
|
39
|
-
roles: [:root]
|
40
|
-
},
|
41
|
-
admin: {
|
42
|
-
id: 1,
|
43
|
-
name: 'admin',
|
44
|
-
email: nil,
|
45
|
-
phone: nil,
|
46
|
-
active: true,
|
47
|
-
user_name: :admin,
|
48
|
-
password: 'test',
|
49
|
-
roles: [:admin]
|
50
|
-
},
|
51
|
-
user: {
|
52
|
-
id: 2,
|
53
|
-
name: 'user',
|
54
|
-
email: nil,
|
55
|
-
phone: nil,
|
56
|
-
active: true,
|
57
|
-
user_name: :user,
|
58
|
-
password: 'test',
|
59
|
-
roles: [:user]
|
60
|
-
}
|
61
|
-
}
|
62
|
-
@models = models_hash # (parameter models_hash usually from Schema::Base.models)
|
9
|
+
def initialize sinatra, cache, db, user_model, models, roles, schema_filters, role_schema_filters, role_query_filters, restriction_query_filters, user_query_filters
|
10
|
+
@sinatra = sinatra
|
11
|
+
@cache = cache
|
12
|
+
@db = db
|
13
|
+
@user_model = user_model
|
14
|
+
@models = models # (parameter models usually from Schema::Base.models)
|
15
|
+
@roles = roles
|
63
16
|
@role_permissions = {}
|
64
17
|
@role_restrictions = {}
|
65
|
-
@roles.each do |role|
|
66
|
-
@role_permissions[role
|
67
|
-
@role_restrictions[role
|
18
|
+
@roles.each do |key, role|
|
19
|
+
@role_permissions[role[:name]] = role[:permissions]
|
20
|
+
@role_restrictions[role[:name]] = role[:restrictions]
|
68
21
|
end
|
69
|
-
@
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
}
|
75
|
-
}
|
76
|
-
@schema_filters = lambda {|identity|
|
77
|
-
filters = {
|
78
|
-
allow_all_tables: {allow: [table: /./]},
|
79
|
-
hide_nonpublic_tables: {allow: [{table: /./}], deny: [{table: /^utility_/}, {table: /^temp_/}]}
|
80
|
-
}
|
81
|
-
}
|
82
|
-
@role_query_filters = lambda {|identity|
|
83
|
-
filters = {
|
84
|
-
admin: [
|
85
|
-
{table_name: :table_i_want_to_filter, sub_select: "select id from table_i_want_to_filter where a = b"}
|
86
|
-
]
|
87
|
-
# more...
|
88
|
-
}
|
89
|
-
}
|
90
|
-
@restriction_query_filters = lambda {|identity|
|
91
|
-
filters = {
|
92
|
-
me_related:[
|
93
|
-
{table_name: :user, sub_select: "select id from user where id = #{identity.user.id}"}
|
94
|
-
]
|
95
|
-
}
|
96
|
-
}
|
97
|
-
@user_query_filters = lambda {|identity|
|
98
|
-
filters = {}
|
99
|
-
}
|
22
|
+
@schema_filters = schema_filters
|
23
|
+
@role_schema_filters = role_schema_filters
|
24
|
+
@role_query_filters = role_query_filters
|
25
|
+
@restriction_query_filters = restriction_query_filters
|
26
|
+
@user_query_filters = user_query_filters
|
100
27
|
end
|
101
28
|
|
102
29
|
def get_user(user_name, password, no_cache=false)
|
103
30
|
lower_case_user_name = nil
|
104
31
|
lower_case_user_name = user_name.downcase.to_sym if user_name
|
105
|
-
|
106
|
-
user =
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
32
|
+
cache_key = "#{lower_case_user_name}.#{password}"
|
33
|
+
user = @cache[cache_key]
|
34
|
+
if user == nil or no_cache
|
35
|
+
hash = JunglePath::SQL::User.by_user_name(@db, user_name)
|
36
|
+
puts "hash: #{hash}."
|
37
|
+
#ds = @db.base["select * from \"user\" where user_name = ?", lower_case_user_name]
|
38
|
+
#hash = ds.first
|
39
|
+
user = @user_model.new(hash, false) if hash
|
40
|
+
@sinatra.halt 401, "Unauthorized" unless user
|
41
|
+
@sinatra.halt 401, "Unauthorized: user #{user.user_name} is not marked as active." unless user.active
|
42
|
+
user.is_valid = JunglePath::Authentication::PasswordHash.validate_password(password, user.hash)
|
43
|
+
user.password = password
|
44
|
+
@cache[cache_key] = user if user
|
45
|
+
end
|
111
46
|
user
|
112
47
|
end
|
113
48
|
|
114
|
-
def
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
49
|
+
def get_user_by_key(key, no_cache=false, password=nil)
|
50
|
+
cache_key = "#{key}."
|
51
|
+
user = @cache[cache_key]
|
52
|
+
if user == nil or no_cache
|
53
|
+
hash = JunglePath::SQL::User.by_key(@db, key)
|
54
|
+
puts "hash: #{hash}."
|
55
|
+
#ds = @db.base['select * from "user" where id in (select user_id from key where key = ?)', key]
|
56
|
+
#hash = ds.first
|
57
|
+
user = @user_model.new(hash, false) if hash
|
58
|
+
@sinatra.halt 401, "Unauthorized" unless user
|
59
|
+
@sinatra.halt 401, "Unauthorized: user #{user.user_name} is not marked as active." unless user.active
|
60
|
+
@cache[cache_key] = user if user
|
120
61
|
end
|
121
|
-
|
62
|
+
user
|
63
|
+
get_user(user_name, password, no_cache)
|
64
|
+
end
|
65
|
+
|
66
|
+
def get_role(identity, no_cache=false)
|
67
|
+
@roles[identity.user.role.to_sym]
|
122
68
|
end
|
123
69
|
|
124
|
-
def
|
125
|
-
|
70
|
+
def get_authorization_filter(identity, no_cache=false)
|
71
|
+
JunglePath::Authorization::Filter.new([identity.role], @models, @role_permissions, @role_restrictions, @role_schema_filters.call(identity), @schema_filters.call(identity))
|
126
72
|
end
|
127
73
|
|
128
74
|
def get_query_filters(identity, no_cache=false)
|
129
75
|
filters = []
|
130
|
-
@role_query_filters.call(identity).each do |filter|
|
76
|
+
@role_query_filters.call(identity).each do |key, filter|
|
131
77
|
filters << filter
|
132
78
|
end
|
133
|
-
@restriction_query_filters.call(identity).each do |filter|
|
79
|
+
@restriction_query_filters.call(identity).each do |key, filter|
|
134
80
|
filters << filter
|
135
81
|
end
|
136
|
-
@user_query_filters.call(identity).each do |filter|
|
82
|
+
@user_query_filters.call(identity).each do |key, filter|
|
137
83
|
filters << filter
|
138
84
|
end
|
139
85
|
filters
|