您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

application.rb 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. # redMine - project management software
  2. # Copyright (C) 2006 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. class ApplicationController < ActionController::Base
  18. before_filter :check_if_login_required, :set_localization
  19. def logged_in_user=(user)
  20. @logged_in_user = user
  21. session[:user_id] = (user ? user.id : nil)
  22. end
  23. def logged_in_user
  24. if session[:user_id]
  25. @logged_in_user ||= User.find(session[:user_id], :include => :memberships)
  26. else
  27. nil
  28. end
  29. end
  30. # check if login is globally required to access the application
  31. def check_if_login_required
  32. require_login if $RDM_LOGIN_REQUIRED
  33. end
  34. def set_localization
  35. lang = begin
  36. if self.logged_in_user and self.logged_in_user.language and !self.logged_in_user.language.empty? and GLoc.valid_languages.include? self.logged_in_user.language.to_sym
  37. self.logged_in_user.language
  38. elsif request.env['HTTP_ACCEPT_LANGUAGE']
  39. accept_lang = parse_qvalues(request.env['HTTP_ACCEPT_LANGUAGE']).first.split('-').first
  40. if accept_lang and !accept_lang.empty? and GLoc.valid_languages.include? accept_lang.to_sym
  41. accept_lang
  42. end
  43. end
  44. rescue
  45. nil
  46. end || $RDM_DEFAULT_LANG
  47. set_language_if_valid(lang)
  48. end
  49. def require_login
  50. unless self.logged_in_user
  51. store_location
  52. redirect_to :controller => "account", :action => "login"
  53. return false
  54. end
  55. true
  56. end
  57. def require_admin
  58. return unless require_login
  59. unless self.logged_in_user.admin?
  60. render :nothing => true, :status => 403
  61. return false
  62. end
  63. true
  64. end
  65. # authorizes the user for the requested action.
  66. def authorize
  67. # check if action is allowed on public projects
  68. if @project.is_public? and Permission.allowed_to_public "%s/%s" % [ @params[:controller], @params[:action] ]
  69. return true
  70. end
  71. # if action is not public, force login
  72. return unless require_login
  73. # admin is always authorized
  74. return true if self.logged_in_user.admin?
  75. # if not admin, check membership permission
  76. @user_membership ||= Member.find(:first, :conditions => ["user_id=? and project_id=?", self.logged_in_user.id, @project.id])
  77. if @user_membership and Permission.allowed_to_role( "%s/%s" % [ @params[:controller], @params[:action] ], @user_membership.role_id )
  78. return true
  79. end
  80. render :nothing => true, :status => 403
  81. false
  82. end
  83. # store current uri in session.
  84. # return to this location by calling redirect_back_or_default
  85. def store_location
  86. session[:return_to] = @request.request_uri
  87. end
  88. # move to the last store_location call or to the passed default one
  89. def redirect_back_or_default(default)
  90. if session[:return_to].nil?
  91. redirect_to default
  92. else
  93. redirect_to_url session[:return_to]
  94. session[:return_to] = nil
  95. end
  96. end
  97. # qvalues http header parser
  98. # code taken from webrick
  99. def parse_qvalues(value)
  100. tmp = []
  101. if value
  102. parts = value.split(/,\s*/)
  103. parts.each {|part|
  104. if m = %r{^([^\s,]+?)(?:;\s*q=(\d+(?:\.\d+)?))?$}.match(part)
  105. val = m[1]
  106. q = (m[2] or 1).to_f
  107. tmp.push([val, q])
  108. end
  109. }
  110. tmp = tmp.sort_by{|val, q| -q}
  111. tmp.collect!{|val, q| val}
  112. end
  113. return tmp
  114. end
  115. end