Class: Fradium
- Inherits:
-
Object
show all
- Defined in:
- lib/fradium.rb,
lib/fradium/version.rb
Defined Under Namespace
Classes: CorruptedUserDatabaseError, UserAlreadyExistsError, UserNotFoundError, UsernameEmptyError
Constant Summary
collapse
- VERSION =
"0.1.6"
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(params) ⇒ Fradium
Returns a new instance of Fradium.
12
13
14
15
|
# File 'lib/fradium.rb', line 12
def initialize(params)
@params = params
@sequel = Sequel.connect(@params)
end
|
Class Method Details
.generate_random_password(length = 10) ⇒ Object
119
120
121
122
123
124
125
|
# File 'lib/fradium.rb', line 119
def self.generate_random_password(length=10)
r = SecureRandom.urlsafe_base64.delete('-_')
while r.length < length
r << SecureRandom.urlsafe_base64.delete('-_')
end
r[0..length-1]
end
|
Instance Method Details
#all_users ⇒ Object
21
22
23
|
# File 'lib/fradium.rb', line 21
def all_users
@sequel[:radcheck].where{attribute.like '%-Password'}
end
|
#create_user(username) ⇒ Object
#dbconsole ⇒ Object
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
# File 'lib/fradium.rb', line 98
def dbconsole
case @sequel.adapter_scheme
when :mysql2
Kernel.exec({'MYSQL_PWD' => @params['password']},
'mysql',
"--pager=less -SF",
"--user=#{@params['username']}",
"--host=#{@params['host']}" ,
"#{@params['database']}")
when :postgres
Kernel.exec({'PGPASSWORD' => @params['password']},
'psql',
"--username=#{@params['username']}",
"--dbname=#{@params['database']}",
"--host=#{@params['host']}")
when :sqlite
Kernel.exec('sqlite3', @params)
end
end
|
#expire_user(username) ⇒ Object
62
63
64
|
# File 'lib/fradium.rb', line 62
def expire_user(username)
set_expiration(username, Time.now)
end
|
#expired_users ⇒ Object
55
56
57
58
59
60
|
# File 'lib/fradium.rb', line 55
def expired_users
now = Time.now
id = @sequel[:radcheck].where(attribute: 'Expiration').collect{|e| e[:id] if Time.now > Time.parse(e[:value])}
@sequel[:radcheck].where(id: id)
end
|
#expiry ⇒ Object
51
52
53
|
# File 'lib/fradium.rb', line 51
def expiry
@sequel[:radcheck].where(attribute: 'Expiration')
end
|
#find_user(username) ⇒ Object
36
37
38
39
|
# File 'lib/fradium.rb', line 36
def find_user(username)
raise UsernameEmptyError if username&.empty?
reult = @sequel[:radcheck].where(username: username).where{attribute.like '%-Password'}
end
|
#is_expired?(username) ⇒ Boolean
72
73
74
75
76
|
# File 'lib/fradium.rb', line 72
def is_expired?(username)
expiration_date = query_expiration(username)&.fetch('value')
return false if expiration_date.nil? || expiration_date.empty? Time.now > Time.parse(expiration_date)
end
|
#modify_user(username) ⇒ Object
#query_expiration(username) ⇒ Object
129
130
131
132
133
134
135
136
137
138
139
140
|
# File 'lib/fradium.rb', line 129
def query_expiration(username)
raise UsernameEmptyError if username&.empty?
raise UserNotFoundError unless user_exists?(username)
r = @sequel[:radcheck].where(username: username, attribute: 'Expiration')
raise CorruptedUserDatabaseError if r.count > 1
return nil if r.count == 0 return nil if r&.first[:value]&.empty?
r
end
|
#set_expiration(username, expiration_date) ⇒ Object
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
# File 'lib/fradium.rb', line 78
def set_expiration(username, expiration_date)
expiration_info = query_expiration(username)
value = ''
if expiration_date.instance_of?(Time)
value = expiration_date.strftime("%d %b %Y %H:%M:%S")
else
value = Time.parse(expiration_date).strftime("%d %b %Y %H:%M:%S")
end
if expiration_info.nil? @sequel[:radcheck].insert(username: username,
attribute: 'Expiration',
op: ':=',
value: value)
else expiration_info.update(value: value)
end
end
|
#unexpire_user(username) ⇒ Object
66
67
68
69
70
|
# File 'lib/fradium.rb', line 66
def unexpire_user(username)
raise UsernameEmptyError if username&.empty?
raise UserNotFoundError unless user_exists?(username)
@sequel[:radcheck].where(username: username, attribute: 'Expiration').delete
end
|
#user_exists?(username) ⇒ Boolean
17
18
19
|
# File 'lib/fradium.rb', line 17
def user_exists?(username)
find_user(username).count > 0
end
|