Skip to content
Browse files

Added OHM. More refactoring. Users must log-in to view sensitive data…

…. Default username / password is "admin" / "admin"
  • Loading branch information...
1 parent 35ac3c1 commit 34b69cac20ceb1e5e369b20cd7be635b12728b00 @ekosz committed
Showing with 93 additions and 70 deletions.
  1. +2 −0 Gemfile
  2. +7 −0 Gemfile.lock
  3. +3 −1 lib/models/group.rb
  4. +4 −4 lib/models/number.rb
  5. +3 −62 lib/models/pca_base.rb
  6. +20 −3 lib/models/user.rb
  7. +6 −0 lib/routes/general.rb
  8. +1 −0 lib/routes/init.rb
  9. +19 −0 lib/routes/login.rb
  10. +9 −0 lib/views/layout.haml
  11. +12 −0 lib/views/login.haml
  12. +7 −0 main.rb
View
2 Gemfile
@@ -5,6 +5,8 @@ gem "haml"
gem "redis"
gem "builder"
gem "json"
+gem 'ohm'
+gem 'shield'
group :test do
gem "rspec", ">= 2.0"
View
7 Gemfile.lock
@@ -35,7 +35,11 @@ GEM
json (1.4.6)
json_pure (1.5.1)
mime-types (1.16)
+ nest (1.1.0)
+ redis (~> 2.1)
nokogiri (1.4.4)
+ ohm (0.1.3)
+ nest (~> 1.0)
rack (1.2.1)
rack-test (0.5.7)
rack (>= 1.0)
@@ -55,6 +59,7 @@ GEM
ffi (~> 0.6.3)
json_pure
rubyzip
+ shield (0.0.3)
sinatra (1.1.2)
rack (~> 1.1)
tilt (~> 1.2)
@@ -78,7 +83,9 @@ DEPENDENCIES
factory_girl (>= 2.0.0.beta2)
haml
json
+ ohm
rack-test
redis
rspec (>= 2.0)
+ shield
sinatra
View
4 lib/models/group.rb
@@ -3,6 +3,8 @@
class Group < PCABase
- attr_accessor :name, :permission, :members
+ attribute :name
+ attribute :permission
+ set :members, Number
end
View
8 lib/models/number.rb
@@ -3,10 +3,10 @@
class Number < PCABase
- attr_accessor :name, :number, :permission
+ attribute :name
+ attribute :number
+ attribute :permission
- def initialize(arg)
- super(arg)
- end
+ set :groups, Group
end
View
65 lib/models/pca_base.rb
@@ -2,58 +2,16 @@
#Author: Eric Koslow
require 'json'
+require 'ohm'
-class PCABase
-
- attr_accessor :id
-
- def initialize(arg = {})
- if arg.is_a? Hash
- create_from_hash(arg)
- else
- raise InvalidArgument, arg
- end
- end
-
- def self.all
- array = Array.new
- Main::REDIS.keys(self.to_s.downcase+':*').each do |key|
- array << self.lookup_from_id(key.split(':')[1])
- end
- return array
- end
+class PCABase < Ohm::Model
def self.accessors
instance_methods(false).keep_if {|v| v=~/\w+=/}.map {|m| '@'+m.to_s[0..-2]}
end
def self.lookup_from_id(id)
- id = self.to_s.downcase+':'+id
- data = Main::REDIS.get id
- if data.nil?
- raise IdNotFound, id
- end
- data = JSON.parse data
- self.new(data)
- end
-
- def save
- unless @id.nil?
- Main::REDIS.set self.class.to_s.downcase+':'+@id, self.to_json
- else
- raise InvalidObjectError, "ID can't be nil"
- end
- end
-
- def delete
- Main::REDIS.del self.class.to_s.downcase+':'+@id
- end
-
- def to_json(*opt)
- hash = Hash.new
- vars = self.instance_variables
- vars.each {|var| hash[var.to_s[1..-1]] = instance_variable_get(var)}
- hash.to_json(*opt)
+ self[id]
end
def try?(sym, *args)
@@ -63,21 +21,4 @@ def try?(sym, *args)
false
end
end
-
- private
-
- def create_from_hash(arg)
- arg.each_pair {|key, val| self.instance_variable_set('@'+key,val)}
- if @id.nil?
- @id = generate_id
- end
- end
-
-
- def generate_id
- chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
- size = 7
- Array.new(size){||chars[rand(chars.size)]}.join
- end
-
end
View
23 lib/models/user.rb
@@ -4,7 +4,15 @@
require 'time'
class User < PCABase
- attr_accessor :name, :number, :extention, :gcal_url
+ extend Shield::Model
+
+ attribute :name
+ attribute :username
+ index :username
+ attribute :crypted_password
+ attribute :number
+ attribute :extention
+ attribute :gcal_url
def inilize(arg={})
if arg.is_a? Hash
@@ -16,9 +24,13 @@ def inilize(arg={})
end
def self.lookup_from_extention(ex)
- data = Main::REDIS.get(Main::REDIS.get('extention:'+ex.to_s))
- create_from_hash JSON.parse(data)
+ User.find(:extention=>ex).first
+ end
+
+ def self.fetch(username)
+ find(:username=>username).first
end
+
def available?
if @gcal_url
now = Time.now.utc.xmlschema
@@ -31,6 +43,11 @@ def available?
return true
end
end
+
+ def password=(password)
+ write_local(:crypted_password, Shield::Password.encrypt(password))
+ @password = password
+ end
private
View
6 lib/routes/general.rb
@@ -2,6 +2,7 @@ class Main < Sinatra::Base
# All of an object
# i.e. /numbers, /users, /groups
get %r{^\/([^\/]+)s$} do |obj|
+ ensure_authenticated(User)
instance_variable_set('@gens', Kernel.const_get(obj.capitalize).all)
@new_path = request.path_info+'/new'
haml :generals
@@ -10,6 +11,7 @@ class Main < Sinatra::Base
# Create a new object
# i.e. /numbers/new, /users/new, /groups/new
get %r{^\/([^\/]+)s\/new$} do |obj|
+ ensure_authenticated(User)
@class = Kernel.const_get(obj.capitalize)
haml :general_new
end
@@ -17,6 +19,7 @@ class Main < Sinatra::Base
# An individual Object
# i.e. /number/5, /user/ekosz, /group/CSH
get %r{^\/([^\/]+[^s])\/([^\/.]+)$} do |obj, id|
+ ensure_authenticated(User)
self.instance_variable_set('@gen',
Kernel.const_get(obj.capitalize).lookup_from_id(id))
@@ -26,6 +29,7 @@ class Main < Sinatra::Base
# Edit an exsiting object
# i.e. /number/5/edit, /user/ekosz/edit, /group/CSH/edit
get %r{^\/([^\/]+[^s])\/([^\/]+)\/edit$} do |obj, id|
+ ensure_authenticated(User)
self.instance_variable_set('@gen',
Kernel.const_get(obj.capitalize).lookup_from_id(id))
haml :general_edit
@@ -34,6 +38,7 @@ class Main < Sinatra::Base
# Create a new obect
# i.e. /numbers/new, /users/new, /groups/new
post %r{^\/([^\/]+)s\/new$} do |obj|
+ ensure_authenticated(User)
request.body.rewind # in case someone already read it
self.instance_variable_set('@'+obj,
Kernel.const_get(obj.capitalize).new(request.POST))
@@ -45,6 +50,7 @@ class Main < Sinatra::Base
# Edit an exsisting object
# i.e. /number/5/edit, /user/ekosz/edit, /group/CSH/edit
put %r{^\/([^\/]+[^s])\/([^\/]+)\/edit$} do |obj, id|
+ #ensure_authenticated(User)
#TODO: Fill in
end
end
View
1 lib/routes/init.rb
@@ -1,3 +1,4 @@
require_relative 'main'
+require_relative 'login'
require_relative 'general'
require_relative 'twilio'
View
19 lib/routes/login.rb
@@ -0,0 +1,19 @@
+class Main < Sinatra::Base
+ get '/login' do
+ haml :login
+ end
+
+ post '/login' do
+ if login(User, params[:username], params[:password])
+ redirect '/numbers'
+ else
+ @error = "Wrong username / password combo"
+ haml :login
+ end
+ end
+
+ get '/logout' do
+ logout(User)
+ redirect '/'
+ end
+end
View
9 lib/views/layout.haml
@@ -33,6 +33,15 @@
%body
+ %header
+ - if !! authenticated(User)
+ = authenticated(User).name
+ - else
+ %a{:herf=>'/login'}
+ Log In!
+ |
+ %a{:href=>'/logout'}
+ Log out :(
= yield
%footer
%a{:href=>'/'}
View
12 lib/views/login.haml
@@ -0,0 +1,12 @@
+%h2 Log In!
+
+%span{:style=>"color:red; text-size:120%"}=@errors if @errors
+%form{:action=>'', :method=>'POST'}
+ %p
+ %label{:name=>'username'} Username:
+ %input{:type=>'text', :name=>'username'}
+ %p
+ %label{:name=>'password'} Password:
+ %input{:type=>'password', :name=>'password'}
+ %p
+ %input{:type=>'submit'}
View
7 main.rb
@@ -7,6 +7,8 @@
require 'sinatra'
require 'haml'
require 'redis'
+require 'ohm'
+require 'shield'
# Helpers
require_relative 'lib/helpers/init'
@@ -14,6 +16,7 @@
class Main < Sinatra::Base
helpers Sinatra::Partials, Sinatra::RedirectWithObjects, Sinatra::TwilioHelpers
+ helpers Shield::Helpers
configure :development, :test do
REDIS = Redis.new
@@ -45,3 +48,7 @@ class Main < Sinatra::Base
require_relative 'lib/models/init'
# Routes
require_relative 'lib/routes/init'
+
+if User.find(:username=>'admin').size == 0
+ User.create(:username=>'admin', :password=>'admin')
+end

0 comments on commit 34b69ca

Please sign in to comment.
Something went wrong with that request. Please try again.