Skip to content

Commit

Permalink
one session for all users fix: github.com/sailorproject/sailor/issues…
Browse files Browse the repository at this point in the history
  • Loading branch information
d9k committed Jul 17, 2018
1 parent 8cf4c5b commit 7eb1211
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 12 deletions.
8 changes: 4 additions & 4 deletions access.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
-- access module can't be required before request init
-- so this module is used to require 'sailor.access' and setup it's settings
local conf = require 'conf.conf'
--local conf = require 'conf.conf'

local access = require 'sailor.access'
access.settings(conf['access_module'])
return access
--local access = require 'sailor.access'
--access.settings(conf['access_module'])
--return access
7 changes: 5 additions & 2 deletions controllers/auth.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
local access = require 'access'
local session = require 'sailor.session'
local pformat = require 'thirdparty_libs.pprint'.pformat

--local conf = require 'conf.conf'
Expand All @@ -9,6 +7,9 @@ local pformat = require 'thirdparty_libs.pprint'.pformat
local auth = {}

function auth.login(page)
-- modules are not reloaded so reload again in action
local access = require 'sailor.access'
local session = require 'sailor.session'

local login = page.POST.login or ''
local password = page.POST.password or ''
Expand All @@ -30,6 +31,8 @@ function auth.login(page)
end

function auth.logout(page)
local access = require 'sailor.access'
local session = require 'sailor.session'
access.logout()

page:render('login', {logout = true, session_data = session_data})
Expand Down
2 changes: 1 addition & 1 deletion controllers/test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ local pretty_format = require "thirdparty_libs.pprint".pformat
--if breakpoints then require('mobdebug').start('127.0.0.1') end

local sailor = require 'sailor'
local access = require 'sailor.access'

local db = require 'sailor.db'
local sailor_helpers = require 'helpers.sailor'
local json = require('cjson')
Expand Down
2 changes: 1 addition & 1 deletion helpers/sailor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
local M = {}

M.get_user = function()
local access = require 'access'
-- local access = require 'sailor.access'
local session = require 'sailor.session'

local user = nil
Expand Down
22 changes: 21 additions & 1 deletion index.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ local log_from_config = require 'log_from_config'
local redis_adapter = require 'local_libs.redis_adapter'

--require 'monkey_patching.sailor_page'
require 'override_modules.override_map'

--require 'monkey_patching.sailor_db_luasql_common'
require 'monkey_patching.sailor'

Expand All @@ -19,5 +19,25 @@ require 'monkey_patching.sailor'
sailor.log = log_from_config.log_from_config(conf.log)
sailor.redis = redis_adapter(conf.redis)

sailor.before_launch = function ()
-- sailor.log:info('before launch')

-- models variables are cached by xavante
-- reset due to fields in modules cache
package.loaded['sailor.access'] = nil
package.loaded['sailor.session'] = nil
package.loaded['override_modules.sailor_session'] = nil
package.loaded['web_utils.session'] = nil
package.loaded['override_modules.override_map'] = nil

require 'override_modules.override_map'
end

sailor.after_init = function ()
-- sailor.log:info('after init')

local access = require 'sailor.access'
access.settings(conf['access_module'])
end

sailor.launch()
4 changes: 3 additions & 1 deletion models/user.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

local user = {}
local val = require 'valua'
local access = require 'sailor.access'
--local access = require 'sailor.access'

-- Attributes and their validation rules
user.attributes = {
Expand All @@ -25,6 +25,8 @@ user.relations = {
-- Public Methods
--function user.test() return "test" end

-- TODO! access must be required locally because is cached by xavante

--function user.authenticate(login,password,use_hashing)
-- if use_hashing == nil then use_hashing = true end
-- access.settings({model = 'user', hashing = use_hashing})
Expand Down
38 changes: 38 additions & 0 deletions monkey_patching/sailor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,42 @@ function sailor.route(page)

return res or httpd.OK or page.r.status or 200
end
end

function sailor.launch(native_request)
-- print('on sailor_launch')
-- local inspect = require 'inspect'
-- print('on sailor_launch: sailor.access: ' .. inspect(package.loaded['sailor.access']))
-- package.loaded['sailor.access'] = nil

print('overriden sailor launch')

if sailor.before_launch then
sailor.before_launch()
end

if apr_table ~= nil then
-- This is Apache with mod_lua
-- Sets a handle function to be called by mod_lua
httpd = apache2
handle = sailor.handle_request
else
-- This is a non-Apache (such as Nginx, Lighttpd, etc) or
-- Apache with CGILua or mod_pLua
-- Handled by Remy extension
httpd = remy.httpd
sailor.remy_mode = remy.init(sailor.remy_mode, native_request)
remy.contentheader('text/html')
remy.run(sailor.handle_request)
end
end


function sailor.handle_request(r)
r.content_type = "text/html"
local page = sailor.init(r)
if sailor.after_init then
sailor.after_init()
end
return sailor.route(page)
end
3 changes: 3 additions & 0 deletions override_modules/override_map.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
--local sailor = require 'sailor'

--sailor.log:info('override_modules.override_map reloaded')
package.loaded['sailor.page'] = require 'override_modules.sailor_page'
package.loaded['sailor.session'] = require 'override_modules.sailor_session'
package.loaded['sailor.form'] = require 'override_modules.sailor_form'
Expand Down
19 changes: 17 additions & 2 deletions override_modules/sailor_session.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ local ID_NAME = "SAILORSESSID"

session.id = nil

--sailor.log:info('sailor session overriden reloaded')

local sailor_init_complete = false

session.after_sailor_init = function()
Expand All @@ -37,12 +39,21 @@ end

function session.open (r)
session.after_sailor_init()
-- local log = function(s)
-- sailor.log:info('session.open: ' .. s)
-- end

-- log('start')
local inspect = require 'inspect'

-- log('session id: ' .. inspect(session.id))

if session.id then
return session.id
end

local id = cookie.get(r,ID_NAME )
local id = cookie.get(r, ID_NAME)
-- log('id after cookie get: ' .. (id or ''))
if not id then
session.new(r)
else
Expand All @@ -55,7 +66,10 @@ function session.open (r)
end

session.cleanup()
return id
-- log('end '..session.id)
-- bug! there was:
-- return id
return session.id
end

function session.destroy (r)
Expand All @@ -81,6 +95,7 @@ end

local save = session.save
function session.save(data)
-- sailor.log:info('session.save to '..session.id)
session.after_sailor_init()

save(session.id,data)
Expand Down

0 comments on commit 7eb1211

Please sign in to comment.