lockdown 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/lockdown.rb ADDED
@@ -0,0 +1,172 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ module Lockdown
5
+ class << self
6
+ def format_controller_action(url)
7
+ url.split("/").delete_if{|p| p.to_i > 0 || p.length == 0}.join("/")
8
+ url += "/index" unless url =~ /\//
9
+ url
10
+ end
11
+
12
+ def format_controller(ctr)
13
+ ctr.split("/").delete_if{|p| p.length == 0}.join("/")
14
+ end
15
+
16
+ def project_root
17
+ project_related_value("Merb.root", "RAILS_ROOT")
18
+ end
19
+
20
+ def merb_app?
21
+ Object.const_defined?("Merb") && Merb.const_defined?("AbstractController")
22
+ end
23
+
24
+ def rails_app?
25
+ Object.const_defined?("ActionController") && ActionController.const_defined?("Base")
26
+ end
27
+
28
+ def controller_parent
29
+ project_related_value("Merb::Controller", "ActionController::Base")
30
+ end
31
+
32
+ def datamapper_orm?
33
+ Object.const_defined?("DataMapper") && DataMapper.const_defined?("Base")
34
+ end
35
+
36
+ def active_record_orm?
37
+ Object.const_defined?("ActiveRecord") && ActiveRecord.const_defined?("Base")
38
+ end
39
+
40
+ def orm_parent
41
+ if datamapper_orm?
42
+ DataMapper::Base
43
+ elsif active_record_orm?
44
+ ActiveRecord::Base
45
+ else
46
+ raise NotImplementedError, "ORM unknown to Lockdown! Lockdown recognizes DataMapper and ActiveRecord"
47
+ end
48
+ end
49
+
50
+ private
51
+
52
+ def project_related_value(merb_val, rails_val)
53
+ if merb_app?
54
+ eval(merb_val)
55
+ elsif rails_app?
56
+ eval(rails_val)
57
+ else
58
+ raise NotImplementedError, "Project type unkown to Lockdown"
59
+ end
60
+
61
+ end
62
+ end # class block
63
+
64
+ require "lockdown/helper.rb"
65
+ require "lockdown/controller_inspector.rb"
66
+ require "lockdown/controller.rb"
67
+ require "lockdown/model.rb"
68
+ require "lockdown/view.rb"
69
+
70
+ module Permissions#:nodoc:
71
+ class << self
72
+ include Lockdown::ControllerInspector
73
+
74
+ def[](sym)
75
+ raise NameError.new("#{sym} is not defined") unless respond_to?(sym)
76
+ send(sym)
77
+ end
78
+
79
+ def access_rights_for(ary)
80
+ ary.collect{|m| send(m)}.flatten
81
+ end
82
+
83
+ def all
84
+ all_controllers
85
+ end
86
+ end # class block
87
+ end # permissions
88
+
89
+ module UserGroups#:nodoc:
90
+ class << self
91
+ def[](sym)
92
+ permissions(sym).collect{|rec| Lockdown::Permissions[rec]}.flatten
93
+ end
94
+
95
+ def permissions(sym)
96
+ if self.private_records.include?(sym)
97
+ return self.send(sym)
98
+ end
99
+
100
+ static_permissions(sym)
101
+ end
102
+
103
+ def static_permissions(sym)
104
+ raise NameError.new("#{sym} is not defined") unless respond_to?(sym)
105
+ send(sym)
106
+ end
107
+ end # class block
108
+ end # usergroups
109
+
110
+ module Session
111
+ include Lockdown::Helper
112
+
113
+ def nil_lockdown_values
114
+ session.each do |key,value|
115
+ session[key] = nil if key.to_s =~ /^user_|access_|expiry/
116
+ end
117
+ end
118
+
119
+ #
120
+ # Does the current user have access to at least one permission
121
+ # in the user group?
122
+ #
123
+ def current_user_access_in_group?(grp)
124
+ return true if current_user_is_admin?
125
+ Lockdown::UserGroups.permissions(grp).each do |perm|
126
+ return true if access_in_perm?(perm)
127
+ end
128
+ false
129
+ end
130
+
131
+ def current_user_is_admin?
132
+ session[:access_rights] == :all
133
+ end
134
+
135
+ private
136
+
137
+ #
138
+ # session[:user_group] and session[:access_rights] are the keys to Lockdown.
139
+ #
140
+ # session[:access_rights] holds the array of "controller/action" strings
141
+ # allowed for the user.
142
+ #
143
+ #
144
+ def add_lockdown_session_values(user)
145
+ session[:access_rights] = user.access_rights.delete_if{|ar| ar.nil? || ar.strip.length == 0}
146
+ if user.user_groups
147
+ groups = syms_from_names(user.user_groups)
148
+ if groups.include? :administrators
149
+ session[:access_rights] = :all
150
+ end
151
+ end
152
+ end
153
+
154
+ def access_in_perm?(perm)
155
+ Lockdown::Permissions[perm].each do |ar|
156
+ return true if session_access_rights_include?(ar)
157
+ end
158
+ false
159
+ end
160
+
161
+ def session_access_rights_include?(str)
162
+ return false unless session[:access_rights]
163
+ session[:access_rights].include?(str)
164
+ end
165
+ end
166
+ # module Session
167
+ # protected
168
+ # include Lockdown::Session
169
+ #
170
+ # end
171
+ end
172
+
data/script/console ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # File: script/console
3
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
+
5
+ libs = " -r irb/completion"
6
+ # Perhaps use a console_lib to store any extra methods I may want available in the cosole
7
+ # libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
8
+ libs << " -r #{File.dirname(__FILE__) + '/../lib/lockdown.rb'}"
9
+ puts "Loading lockdown gem"
10
+ exec "#{irb} #{libs} --simple-prompt"
data/script/destroy ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/destroy'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Destroy.new.run(ARGV)
data/script/generate ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/generate'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Generate.new.run(ARGV)
data/script/txt2html ADDED
@@ -0,0 +1,82 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ GEM_NAME = 'lockdown' # what ppl will type to install your gem
4
+ RUBYFORGE_PROJECT = 'lockdown'
5
+
6
+ require 'rubygems'
7
+ begin
8
+ require 'newgem'
9
+ require 'rubyforge'
10
+ rescue LoadError
11
+ puts "\n\nGenerating the website requires the newgem RubyGem"
12
+ puts "Install: gem install newgem\n\n"
13
+ exit(1)
14
+ end
15
+ require 'redcloth'
16
+ require 'syntax/convertors/html'
17
+ require 'erb'
18
+ require File.dirname(__FILE__) + "/../lib/#{GEM_NAME}/version.rb"
19
+
20
+ version = Lockdown::VERSION::STRING
21
+ download = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}"
22
+
23
+ def rubyforge_project_id
24
+ RubyForge.new.autoconfig["group_ids"][RUBYFORGE_PROJECT]
25
+ end
26
+
27
+ class Fixnum
28
+ def ordinal
29
+ # teens
30
+ return 'th' if (10..19).include?(self % 100)
31
+ # others
32
+ case self % 10
33
+ when 1: return 'st'
34
+ when 2: return 'nd'
35
+ when 3: return 'rd'
36
+ else return 'th'
37
+ end
38
+ end
39
+ end
40
+
41
+ class Time
42
+ def pretty
43
+ return "#{mday}#{mday.ordinal} #{strftime('%B')} #{year}"
44
+ end
45
+ end
46
+
47
+ def convert_syntax(syntax, source)
48
+ return Syntax::Convertors::HTML.for_syntax(syntax).convert(source).gsub(%r!^<pre>|</pre>$!,'')
49
+ end
50
+
51
+ if ARGV.length >= 1
52
+ src, template = ARGV
53
+ template ||= File.join(File.dirname(__FILE__), '/../website/template.html.erb')
54
+ else
55
+ puts("Usage: #{File.split($0).last} source.txt [template.html.erb] > output.html")
56
+ exit!
57
+ end
58
+
59
+ template = ERB.new(File.open(template).read)
60
+
61
+ title = nil
62
+ body = nil
63
+ File.open(src) do |fsrc|
64
+ title_text = fsrc.readline
65
+ body_text_template = fsrc.read
66
+ body_text = ERB.new(body_text_template).result(binding)
67
+ syntax_items = []
68
+ body_text.gsub!(%r!<(pre|code)[^>]*?syntax=['"]([^'"]+)[^>]*>(.*?)</\1>!m){
69
+ ident = syntax_items.length
70
+ element, syntax, source = $1, $2, $3
71
+ syntax_items << "<#{element} class='syntax'>#{convert_syntax(syntax, source)}</#{element}>"
72
+ "syntax-temp-#{ident}"
73
+ }
74
+ title = RedCloth.new(title_text).to_html.gsub(%r!<.*?>!,'').strip
75
+ body = RedCloth.new(body_text).to_html
76
+ body.gsub!(%r!(?:<pre><code>)?syntax-temp-(\d+)(?:</code></pre>)?!){ syntax_items[$1.to_i] }
77
+ end
78
+ stat = File.stat(src)
79
+ created = stat.ctime
80
+ modified = stat.mtime
81
+
82
+ $stdout << template.result(binding)