diary/diary_env.example.rb

81 lines
2.2 KiB
Ruby

# SPDX-License-Identifier: Apache-2.0
# Copyright (C) 2021 OKTET Labs Ltd. All rights reserved.
#
# Class DiaryEnv for Diary Management Application.
#
require 'singleton'
require_relative 'ldap_record'
require_relative 'diary_datamapper'
class DiaryEnv
include Singleton
DB_HOST = "localhost"
DB_USERNAME = "diary"
DB_PASSWORD = "diary_pass"
DB_DATABASE = "diary"
LDAP_HOST = 'ldap.example.com'
LDAP_PORT = 389
LDAP_ROOT = "ou=People,dc=example,dc=com"
LDAP_VER = 3
LDAP_BIND_DN = ""
LDAP_BIND_PW = ""
HOME_ORGANIZATION = "Example ORG"
HOME_OU = "Employees"
SMTP_HOST = 'mail.example.com'
SMTP_FROM = 'diary@example.com'
# Maximum age of diary entry (in days) to accept (approval mode only)
DIARY_MAX_AGE = 2
def initialize
@confirmation = Array.new
Person.setup(:host => LDAP_HOST,
:port => LDAP_PORT,
:root => LDAP_ROOT,
:ver => LDAP_VER,
:binddn => LDAP_BIND_DN,
:bindpw => LDAP_BIND_PW,
:key => "uid")
Person.set_local(HOME_ORGANIZATION, HOME_OU)
DataMapper.setup(:adapter => "Mysql2",
:database => DB_DATABASE,
:host => DB_HOST,
:username => DB_USERNAME,
:password => DB_PASSWORD)
cmd = "SELECT nick FROM director"
@director = []
DataMapper.database.query(cmd, as: :hash, symbolize_keys: false).each do |q|
raise "'Director' table is not filled" unless
q and name = q.fetch('nick')
@director.push(Person.new(name))
end
end
def user=(name)
@user = Person.find(name)
raise "Invalid user #{name}" unless @user
@policy = @user.employee? ? EmployeePolicy.new(@user) :
CustomerPolicy.new(@user)
end
def confirmation=(value)
if value.is_a? Array
@confirmation = value
end
end
attr_reader :director, :confirmation, :policy, :user
def self.director
instance.director
end
def self.confirmed?(token)
instance.confirmation.include?(token)
end
end