forked from rodrigorgs/syncgroups-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sguiserver.rb
83 lines (65 loc) · 1.62 KB
/
sguiserver.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env ruby
require 'sinatra/base'
require 'rack/csrf'
require 'sinatra_warden'
require_relative 'lib/sgui.rb'
class Application < Sinatra::Base
register Sinatra::Warden
set :auth_template_renderer, 'erb'
set :auth_success_path, '/groups'
set :auth_failure_path, '/login'
use Rack::Csrf, :raise => true
def initialize(app = nil)
super(app)
if ARGV.length > 0 && ARGV[0] == 'development'
require_relative 'lib/sgui_fake.rb'
@facade = SguiFakeFacade.new(nil)
puts "Running SGUI in development mode"
else
@facade = SguiFacade.new('config/sgui.yml')
puts "Running SGUI in production mode"
end
end
def authenticate
username = params['username']
password = params['password']
if @facade.login(username, password)
session['username'] = username
end
end
def logout
session['username'] = nil
end
def authenticated?
!session['username'].nil?
end
def logged_user
session['username']
end
#############
before do
pass if request.path_info =~ %r{/(login|logout)/?}
authorize!
end
get '/' do
redirect to('/groups')
end
get '/groups' do
@groups = @facade.list_groups_managed_by_user(logged_user)
erb :groups_index
end
before '/groups/:group' do |group|
if [email protected]_access_group(logged_user, group)
halt 401, erb(:error)
end
end
get '/groups/:group' do |group|
@members = @facade.list_group_members(group)
erb :groups_edit
end
post '/groups/:group' do |group|
@facade.update_group(group, params['username'])
erb :groups_show
end
run! if app_file == $0
end