diff options
author | John Ankarström <john@ankarstrom.se> | 2021-03-13 22:05:55 +0100 |
---|---|---|
committer | John Ankarström <john@ankarstrom.se> | 2021-03-13 22:05:55 +0100 |
commit | 69c0dff574cbe13bd3b9c23bde10a685dce16b2a (patch) | |
tree | 92a264e8e1b53afe4832572b79093da599722dea | |
parent | edd1b21f926f0bcec0033b4b481fe12f54029224 (diff) | |
download | comb-master.tar.gz |
-rw-r--r-- | interfaces/http/admin/admin.rb | 22 | ||||
-rw-r--r-- | interfaces/ruby/models/user.rb | 11 | ||||
-rw-r--r-- | interfaces/ruby/ruby.rb | 1 |
3 files changed, 32 insertions, 2 deletions
diff --git a/interfaces/http/admin/admin.rb b/interfaces/http/admin/admin.rb index 347a051..40cbe07 100644 --- a/interfaces/http/admin/admin.rb +++ b/interfaces/http/admin/admin.rb @@ -1,13 +1,31 @@ class AdminInterface < Sinatra::Base enable :sessions + helpers do + def logged_in? + @session != nil + end + end + + before do + @session = Session.where code: session[:code] + end + before ['/admin', '/admin/*'] do - redirect '/login', 303 if not false + redirect '/login', 303 if not logged_in? end - + get '/login' do end + post '/login' do + @session = Session.start(params) + if @session then + else + redirect '/login', 401 + end + end + get '/admin' do @title = $config.title @posts = Post.all diff --git a/interfaces/ruby/models/user.rb b/interfaces/ruby/models/user.rb index 8b88b4a..a251dec 100644 --- a/interfaces/ruby/models/user.rb +++ b/interfaces/ruby/models/user.rb @@ -1,8 +1,19 @@ +require 'bcrypt' + class User < ActiveRecord::Base has_many :posts + has_many :sessions validates_presence_of :username validates_presence_of :password_hash + + def hash(password) + BCrypt::Password.create(password) + end + + def authenticate(password) + BCrypt::Password.new(@password) == password + end end class CreateUserTable < ActiveRecord::Migration[6.0] diff --git a/interfaces/ruby/ruby.rb b/interfaces/ruby/ruby.rb index 2e6c5d9..3a6a980 100644 --- a/interfaces/ruby/ruby.rb +++ b/interfaces/ruby/ruby.rb @@ -8,5 +8,6 @@ ActiveRecord::Base.establish_connection( require_relative 'models/comment' require_relative 'models/config' require_relative 'models/post' +require_relative 'models/session' require_relative 'models/tag' require_relative 'models/user' |