fluke/devops-service/db/mongo/models/user.rb

140 lines
4.0 KiB
Ruby
Raw Normal View History

2015-07-16 17:18:55 +03:00
require "exceptions/invalid_record"
2014-05-08 15:34:26 +04:00
require "exceptions/invalid_command"
require "db/mongo/models/mongo_model"
2015-03-06 12:20:30 +03:00
module Devops
module Model
class User < MongoModel
ROOT_USER_NAME = 'root'
ROOT_PASSWORD = ''
PRIVILEGES = ["r", "w", "x"]
PRIVILEGES_REGEX = /^r?w?x?$/
2015-11-19 14:09:38 +03:00
# attr_accessor :id, :password, :privileges, :email
# types :id => {:type => String, :empty => false},
# :email => {:type => String, :empty => false},
# :password => {:type => String, :empty => true}
2015-03-06 12:20:30 +03:00
2015-11-19 14:09:38 +03:00
set_field_validators :id, [::Validators::FieldValidator::NotNil,
::Validators::FieldValidator::FieldType::String,
2015-11-19 14:09:38 +03:00
::Validators::FieldValidator::Name]
set_field_validators :password, [::Validators::FieldValidator::NotNil,
::Validators::FieldValidator::FieldType::String]
set_field_validators :email, [::Validators::FieldValidator::NotNil,
::Validators::FieldValidator::FieldType::String]
set_field_validators :privileges, [::Validators::FieldValidator::NotNil,
::Validators::FieldValidator::FieldType::Hash]
2015-03-06 12:20:30 +03:00
def initialize p={}
self.id = p['username']
self.email = p['email']
self.password = p['password']
self.privileges = p["privileges"] || self.default_privileges
end
2014-05-08 15:34:26 +04:00
def validate!
validate_id!
validate_password!
validate_email!
validate_privileges!
end
2015-03-06 12:20:30 +03:00
def all_privileges
privileges_with_value("rwx")
end
2014-05-08 15:34:26 +04:00
2015-03-06 12:20:30 +03:00
def default_privileges
privileges_with_value("r", "user" => "")
end
2014-05-08 15:34:26 +04:00
2015-03-06 12:20:30 +03:00
def grant cmd, priv=''
priv='' if priv.nil?
cmd='' if cmd.nil?
2015-03-06 12:20:30 +03:00
if !priv.empty? and PRIVILEGES_REGEX.match(priv).to_s.empty?
raise InvalidCommand.new "Invalid privileges '#{priv}'. Available values are '#{PRIVILEGES.join("', '")}'"
end
raise InvalidPrivileges.new "Can't grant privileges to root" if self.id == ROOT_USER_NAME
case cmd
when "all"
self.privileges.each_key do |key|
self.privileges[key] = priv
end
when ""
self.privileges = self.default_privileges
else
raise InvalidCommand.new "Unsupported command '#{cmd}'" unless self.all_privileges.include?(cmd)
2015-03-06 12:20:30 +03:00
self.privileges[cmd] = priv
end
end
2014-05-08 15:34:26 +04:00
2015-03-06 12:20:30 +03:00
def self.build_from_bson s
user = User.new s
user.id = s["_id"]
user
2014-05-08 15:34:26 +04:00
end
2015-03-06 12:20:30 +03:00
def self.create_from_json json
User.new( JSON.parse(json) )
end
2014-05-08 15:34:26 +04:00
2015-03-06 12:20:30 +03:00
def to_hash_without_id
o = {
"email" => self.email,
"password" => self.password,
"privileges" => self.privileges
}
o
end
2014-05-08 15:34:26 +04:00
2015-07-16 17:18:55 +03:00
def check_privileges cmd, required_privelege
unless PRIVILEGES.include?(required_privelege)
raise InvalidPrivileges.new("Access internal problem with privilege '#{required_privelege}'")
end
2015-07-27 18:27:52 +03:00
# can?(cmd, required_privelege)
unless can?(cmd, required_privelege)
raise InvalidPrivileges.new("Access denied for '#{id}'")
end
2015-03-06 12:20:30 +03:00
end
2014-05-08 15:34:26 +04:00
2015-03-06 12:20:30 +03:00
def self.create_root
root = User.new({'username' => ROOT_USER_NAME, 'password' => ROOT_PASSWORD})
root.privileges = root.all_privileges
root.email = "#{ROOT_USER_NAME}@host"
root
end
2014-05-08 15:34:26 +04:00
2015-03-06 12:20:30 +03:00
private
2015-07-16 17:18:55 +03:00
def can?(command, privilege)
p = self.privileges[command] || []
p.include?(privilege)
end
2015-03-06 12:20:30 +03:00
def privileges_with_value value, options={}
privileges = {}
[
'flavor',
'group',
'image',
'project',
'server',
'key',
'user',
'filter',
'network',
'provider',
'script',
'templates',
'stack_template',
'stack'
].each { |t| privileges.store(t, value) }
privileges.merge(options)
end
2014-05-08 15:34:26 +04:00
2015-03-06 12:20:30 +03:00
end
2014-05-08 15:34:26 +04:00
end
end