mirror of
https://github.com/OpenNebula/one.git
synced 2026-11-21 04:12:37 +09:00
197 lines
5.0 KiB
Ruby
197 lines
5.0 KiB
Ruby
require 'digest/sha1'
|
|
|
|
class Session
|
|
PERMISSIONS = {
|
|
:user => {
|
|
:create => {
|
|
:anonymous => true,
|
|
:user => true,
|
|
:admin => true
|
|
},
|
|
:show => {
|
|
:anonymous => false,
|
|
:user => false,
|
|
:admin => true
|
|
},
|
|
:delete => {
|
|
:anonymous => false,
|
|
:user => false,
|
|
:admin => true
|
|
},
|
|
:update => {
|
|
:anonymous => false,
|
|
:user => false,
|
|
:admin => true
|
|
},
|
|
:list => {
|
|
:anonymous => false,
|
|
:user => false,
|
|
:admin => true
|
|
},
|
|
:enable => {
|
|
:anonymous => false,
|
|
:user => false,
|
|
:admin => true
|
|
},
|
|
:schema => {
|
|
:anonymous => User::SCHEMA,
|
|
:user => User::SCHEMA,
|
|
:admin => User::ADMIN_SCHEMA
|
|
}
|
|
},
|
|
:appliance => {
|
|
:create => {
|
|
:anonymous => false,
|
|
:user => true,
|
|
:admin => true
|
|
},
|
|
:show => {
|
|
:anonymous => true,
|
|
:user => true,
|
|
:admin => true
|
|
},
|
|
:delete => {
|
|
:anonymous => false,
|
|
:user => true,
|
|
:admin => true
|
|
},
|
|
:update => {
|
|
:anonymous => false,
|
|
:user => true,
|
|
:admin => true
|
|
},
|
|
:list => {
|
|
:anonymous => true,
|
|
:user => true,
|
|
:admin => true
|
|
},
|
|
:download => {
|
|
:anonymous => true,
|
|
:user => true,
|
|
:admin => true
|
|
},
|
|
:schema => {
|
|
:anonymous => Appliance::SCHEMA,
|
|
:user => Appliance::SCHEMA,
|
|
:admin => Appliance::ADMIN_SCHEMA
|
|
}
|
|
}
|
|
}
|
|
|
|
def initialize(env)
|
|
@user = authenticate(env)
|
|
end
|
|
|
|
def authorize(env)
|
|
perms = case env["REQUEST_METHOD"]
|
|
when 'GET', 'HEAD'
|
|
case env["PATH_INFO"]
|
|
when /^\/user$/
|
|
PERMISSIONS[:user][:list]
|
|
when /^\/user\/\w+$/
|
|
PERMISSIONS[:user][:show]
|
|
when /^\/appliance$/
|
|
PERMISSIONS[:appliance][:list]
|
|
when /^\/appliance\/\w+$/
|
|
PERMISSIONS[:appliance][:show]
|
|
when /^\/appliance\/\w+\/download$/
|
|
PERMISSIONS[:appliance][:download]
|
|
when /^\/favicon.ico$/
|
|
true
|
|
end
|
|
when 'DELETE'
|
|
case env["PATH_INFO"]
|
|
when /^\/user\/\w+$/
|
|
PERMISSIONS[:user][:delete]
|
|
when /^\/appliance\/\w+$/
|
|
PERMISSIONS[:appliance][:delete]
|
|
end
|
|
when 'PUT'
|
|
case env["PATH_INFO"]
|
|
when /^\/user\/\w+$/
|
|
PERMISSIONS[:user][:update]
|
|
when /^\/appliance\/\w+$/
|
|
PERMISSIONS[:appliance][:update]
|
|
end
|
|
when 'POST'
|
|
case env["PATH_INFO"]
|
|
when /^\/user$/
|
|
PERMISSIONS[:user][:create]
|
|
when /^\/appliance$/
|
|
PERMISSIONS[:appliance][:create]
|
|
when /^\/user\/\w+\/enable$/
|
|
PERMISSIONS[:user][:enable]
|
|
end
|
|
end
|
|
|
|
if perms.instance_of?(Hash)
|
|
perms[role]
|
|
else
|
|
false
|
|
end
|
|
end
|
|
|
|
def schema(resource)
|
|
PERMISSIONS[resource][:schema][role].dup
|
|
end
|
|
|
|
def allowed_catalogs
|
|
if anonymous?
|
|
[Appliance::PUBLIC_CATALOG]
|
|
elsif user?
|
|
if @user['catalogs']
|
|
[Appliance::PUBLIC_CATALOG] + @user['catalogs']
|
|
else
|
|
[Appliance::PUBLIC_CATALOG]
|
|
end
|
|
elsif admin?
|
|
nil
|
|
end
|
|
end
|
|
|
|
def admin?
|
|
role == :admin
|
|
end
|
|
|
|
def user?
|
|
role == :user
|
|
end
|
|
|
|
def anonymous?
|
|
role == :anonymous
|
|
end
|
|
|
|
def role
|
|
if @user.nil?
|
|
:anonymous
|
|
elsif @user['role'] == User::ADMIN_ROLE
|
|
:admin
|
|
else
|
|
:user
|
|
end
|
|
end
|
|
|
|
def name
|
|
@user['username'] if @user
|
|
end
|
|
|
|
# Retrieve the publisher name of the session
|
|
def publisher
|
|
@user['organization'] if @user
|
|
end
|
|
|
|
private
|
|
|
|
def authenticate(env)
|
|
auth = Rack::Auth::Basic::Request.new(env)
|
|
|
|
if auth.provided? && auth.basic?
|
|
username, password = auth.credentials
|
|
|
|
#sha1_pass = Digest::SHA1.hexdigest(password)
|
|
User.retrieve(username, password)
|
|
else
|
|
nil
|
|
end
|
|
end
|
|
end |