PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/app/controllers/simple_controller.rb

https://bitbucket.org/jegt/simple
Ruby | 128 lines | 108 code | 15 blank | 5 comment | 11 complexity | 3a1b804959700960a1d5f1ec98d4398c MD5 | raw file
Possible License(s): LGPL-2.1
  1. # Filters added to this controller apply to all controllers in the application.
  2. # Likewise, all the methods added will be available for all controllers.
  3. require "will_paginate"
  4. class SimpleController < ApplicationController
  5. unloadable
  6. # See ActionController::RequestForgeryProtection for details
  7. # Uncomment the :secret if you're not using the cookie session store
  8. #protect_from_forgery # :secret => '1159dd7a699f6b0ecac00f33cf591e64'
  9. self.allow_forgery_protection = false
  10. before_filter :detect_site
  11. before_filter :load_objects
  12. rescue_from(Simple::NoAccessException) {render :template => 'security/no_access'}
  13. HTML_CONTENT = 'SimpleHtmlContent'
  14. REVIEW_CONTENT = 'ReviewContent'
  15. SITEMAP_CONTENT = 'SimpleSitemapContent'
  16. CONTENT_TYPES = [HTML_CONTENT, REVIEW_CONTENT, SITEMAP_CONTENT]
  17. NEWS_MODULE = 'NewsModule'
  18. NEWSLETTER_SIGNUP_MODULE = 'NewsletterSignupModule'
  19. FAQ_MODULE = 'FaqModule'
  20. BLOG_MODULE = 'BlogModule'
  21. FORM_MODULE = 'FormModule'
  22. SIMPLE_MODULES = [NEWS_MODULE, NEWSLETTER_SIGNUP_MODULE, FAQ_MODULE, BLOG_MODULE, FORM_MODULE]
  23. def detect_site
  24. logger.info(request.host)
  25. name = request.host.split(':')[0]
  26. domain = SimpleDomain.find(:first, :conditions => ["name = ?", name])
  27. if domain
  28. @current_site = domain.site
  29. elsif params[:controller] == 'admin/sessions'
  30. @current_site = nil
  31. else
  32. render :status => 404, :file => "#{RAILS_ROOT}/public/404.html"
  33. return
  34. end
  35. path = request.path.split('/')
  36. if (@current_site.nil? or !@current_site.active) && path[1] != 'admin' && path[1] != 'simplelogin' && path[1] != 'simplelogout'
  37. render :status => 404, :file => "#{RAILS_ROOT}/public/404.html"
  38. end
  39. end
  40. def load_objects
  41. if session[:simple_user_id]
  42. @current_simple_user = SimpleUser.find(session[:simple_user_id])
  43. end
  44. end
  45. def login_required
  46. if !session[:simple_user_id]
  47. redirect_to login_path
  48. end
  49. end
  50. def index
  51. end
  52. def get_urlname(name)
  53. foo = name.downcase.strip
  54. foo.gsub!(/[ÀÁÂÃÄÅâäàãáäå????????????]/,'a')
  55. foo.gsub!(/[ëêéè????????????????????]/,'e')
  56. foo.gsub!(/[ÌÍÎ?Ïiìíî???ï?????????]/,'i')
  57. foo.gsub!(/[ÒÓÔÕÖòóôõ???ö?????????ø???????????????????]/,'o')
  58. foo.gsub!(/[ÙÚÛ?Üùúû???ü????????????????????????]/,'u')
  59. foo.gsub!(/[?ý????ÿ????]/,'y')
  60. foo.gsub!(/[œ]/,'oe')
  61. foo.gsub!(/[Æ??æ]/,'ae')
  62. foo.gsub!(/[ñ??]/,'n')
  63. foo.gsub!(/[Çç]/,'c')
  64. foo.gsub!(/[ß]/,'b')
  65. foo.gsub!(/[œ]/,'oe')
  66. foo.gsub!(/[?]/,'ij')
  67. foo.gsub!(/[\s\\\/\?\.\=\+\&\%\:]/,'_')
  68. foo.gsub!(/[\'\"]/,'')
  69. foo.gsub!(/_+/,'_')
  70. return foo
  71. end
  72. def check_role(role)
  73. if !@current_simple_user
  74. raise Simple::NoAccessException
  75. else
  76. for r in @current_simple_user.roles
  77. if role == SimpleRole::ROLE_SITEADMIN && (r.role == SimpleRole::ROLE_SUPERADMIN or r.role == SimpleRole::ROLE_SITEADMIN)
  78. return true
  79. elsif role == r.role
  80. return true
  81. end
  82. end
  83. raise Simple::NoAccessException
  84. end
  85. end
  86. def must_be_superadmin
  87. check_role(SimpleRole::ROLE_SUPERADMIN)
  88. end
  89. def must_be_siteadmin
  90. check_role(SimpleRole::ROLE_SITEADMIN)
  91. end
  92. def render_layout(layout)
  93. rendered_page = render_to_string :inline => layout.template
  94. rendered_page.gsub!(/\[\[(.*)\]\]/) do |m|
  95. logger.debug "MATCH: #{m} #{$1}"
  96. cc = $1
  97. if SimpleComponent.instance_methods.include?(cc.split('(').first)
  98. eval(cc)
  99. elsif RAILS_ENV == 'development'
  100. "Unknown component: #{cc}"
  101. else
  102. ''
  103. end
  104. end
  105. return rendered_page
  106. end
  107. end