You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

application_controller.rb 23 kB

13 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
13 years ago
​
​
​
​
​
​
​
​
​
​
13 years ago
​
​
​
​
​
​
​
​
​
​
​
​
13 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
13 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
13 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
13 years ago
13 years ago
​
​
​
​
​
​
​
​
​
​
​
​
13 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
13 years ago
​
​
​
​
​
​
​
13 years ago
​
12 years ago
12 years ago
13 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
13 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
13 years ago
​
​
​
​
13 years ago
​
​
​
​
​
​
​
​
13 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
13 years ago
​
​
13 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2013 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. require 'uri'
  18. require 'cgi'
  19. class Unauthorized < Exception; end
  20. class ApplicationController < ActionController::Base
  21. include Redmine::I18n
  22. include Redmine::Pagination
  23. include RoutesHelper
  24. helper :routes
  25. #added by william
  26. helper :watchers
  27. class_attribute :accept_api_auth_actions
  28. class_attribute :accept_rss_auth_actions
  29. class_attribute :model_object
  30. layout 'base'
  31. protect_from_forgery
  32. def handle_unverified_request
  33. super
  34. cookies.delete(autologin_cookie_name)
  35. end
  36. before_filter :session_expiration, :user_setup, :check_if_login_required, :set_localization
  37. before_filter :user_agent
  38. rescue_from ActionController::InvalidAuthenticityToken, :with => :invalid_authenticity_token
  39. rescue_from ::Unauthorized, :with => :deny_access
  40. rescue_from ::ActionView::MissingTemplate, :with => :missing_template
  41. include Redmine::Search::Controller
  42. include Redmine::MenuManager::MenuController
  43. helper Redmine::MenuManager::MenuHelper
  44. def user_agent
  45. logger.info "HTTP_USER_AGENT #{request.env["HTTP_USER_AGENT"]}"
  46. end
  47. def session_expiration
  48. if session[:user_id]
  49. if session_expired? && !try_to_autologin
  50. reset_session
  51. flash[:error] = l(:error_session_expired)
  52. redirect_to signin_url
  53. else
  54. session[:atime] = Time.now.utc.to_i
  55. end
  56. end
  57. end
  58. def session_expired?
  59. if Setting.session_lifetime?
  60. unless session[:ctime] && (Time.now.utc.to_i - session[:ctime].to_i <= Setting.session_lifetime.to_i * 60)
  61. return true
  62. end
  63. end
  64. if Setting.session_timeout?
  65. unless session[:atime] && (Time.now.utc.to_i - session[:atime].to_i <= Setting.session_timeout.to_i * 60)
  66. return true
  67. end
  68. end
  69. false
  70. end
  71. def start_user_session(user)
  72. session[:user_id] = user.id
  73. session[:ctime] = Time.now.utc.to_i
  74. session[:atime] = Time.now.utc.to_i
  75. end
  76. def user_setup
  77. # Check the settings cache for each request
  78. Setting.check_cache
  79. # Find the current user
  80. User.current = find_current_user
  81. logger.info(" Current user: " + (User.current.logged? ? "#{User.current.login} (id=#{User.current.id})" : "anonymous")) if logger
  82. end
  83. # Returns the current user or nil if no user is logged in
  84. # and starts a session if needed
  85. def current_user
  86. find_current_user
  87. end
  88. def find_current_user
  89. user = nil
  90. unless api_request?
  91. if session[:user_id]
  92. # existing session
  93. user = (User.active.find(session[:user_id]) rescue nil)
  94. elsif autologin_user = try_to_autologin
  95. user = autologin_user
  96. elsif params[:format] == 'atom' && params[:key] && request.get? && accept_rss_auth?
  97. # RSS key authentication does not start a session
  98. user = User.find_by_rss_key(params[:key])
  99. end
  100. end
  101. if user.nil? && Setting.rest_api_enabled? && accept_api_auth?
  102. if (key = api_key_from_request)
  103. # Use API key
  104. user = User.find_by_api_key(key)
  105. else
  106. # HTTP Basic, either username/password or API key/random
  107. authenticate_with_http_basic do |username, password|
  108. user = User.try_to_login(username, password) || User.find_by_api_key(username)
  109. end
  110. end
  111. # Switch user if requested by an admin user
  112. if user && user.admin? && (username = api_switch_user_from_request)
  113. su = User.find_by_login(username)
  114. if su && su.active?
  115. logger.info(" User switched by: #{user.login} (id=#{user.id})") if logger
  116. user = su
  117. else
  118. render_error :message => 'Invalid X-Redmine-Switch-User header', :status => 412
  119. end
  120. end
  121. end
  122. user
  123. end
  124. def autologin_cookie_name
  125. Redmine::Configuration['autologin_cookie_name'].presence || 'autologin'
  126. end
  127. def try_to_autologin
  128. if cookies[autologin_cookie_name] && Setting.autologin?
  129. # auto-login feature starts a new session
  130. user = User.try_to_autologin(cookies[autologin_cookie_name])
  131. if user
  132. reset_session
  133. start_user_session(user)
  134. end
  135. user
  136. end
  137. end
  138. # Sets the logged in user
  139. def logged_user=(user)
  140. reset_session
  141. if user && user.is_a?(User)
  142. User.current = user
  143. start_user_session(user)
  144. else
  145. User.current = User.anonymous
  146. end
  147. end
  148. # Logs out current user
  149. def logout_user
  150. if User.current.logged?
  151. cookies.delete(autologin_cookie_name)
  152. Token.delete_all(["user_id = ? AND action = ?", User.current.id, 'autologin'])
  153. self.logged_user = nil
  154. end
  155. end
  156. # check if login is globally required to access the application
  157. def check_if_login_required
  158. # no check needed if user is already logged in
  159. return true if User.current.logged?
  160. require_login if Setting.login_required?
  161. end
  162. def set_localization
  163. lang = nil
  164. if User.current.logged?
  165. lang = find_language(User.current.language)
  166. end
  167. if lang.nil? && request.env['HTTP_ACCEPT_LANGUAGE']
  168. accept_lang = parse_qvalues(request.env['HTTP_ACCEPT_LANGUAGE']).first
  169. if !accept_lang.blank?
  170. accept_lang = accept_lang.downcase
  171. lang = find_language(accept_lang) || find_language(accept_lang.split('-').first)
  172. end
  173. end
  174. lang ||= Setting.default_language
  175. set_language_if_valid(lang)
  176. end
  177. def require_login
  178. if !User.current.logged?
  179. # Extract only the basic url parameters on non-GET requests
  180. if request.get?
  181. url = url_for(params)
  182. else
  183. url = url_for(:controller => params[:controller], :action => params[:action], :id => params[:id], :project_id => params[:project_id])
  184. end
  185. respond_to do |format|
  186. format.html { redirect_to :controller => "account", :action => "login", :back_url => url }
  187. format.atom { redirect_to :controller => "account", :action => "login", :back_url => url }
  188. format.xml { head :unauthorized, 'WWW-Authenticate' => 'Basic realm="Redmine API"' }
  189. format.js { head :unauthorized, 'WWW-Authenticate' => 'Basic realm="Redmine API"' }
  190. format.json { head :unauthorized, 'WWW-Authenticate' => 'Basic realm="Redmine API"' }
  191. end
  192. return false
  193. end
  194. true
  195. end
  196. def require_admin
  197. return unless require_login
  198. if !User.current.admin?
  199. render_403
  200. return false
  201. end
  202. true
  203. end
  204. def deny_access
  205. User.current.logged? ? render_403 : require_login
  206. end
  207. # Authorize the user for the requested action
  208. def authorize(ctrl = params[:controller], action = params[:action], global = false)
  209. #modify by NWB
  210. if @project
  211. allowed = User.current.allowed_to?({:controller => ctrl, :action => action}, @project || @projects, :global => global)
  212. elsif @course
  213. allowed = User.current.allowed_to?({:controller => ctrl, :action => action}, @course || @courses, :global => global)
  214. else
  215. allowed = User.current.allowed_to?({:controller => ctrl, :action => action}, @project || @projects, :global => global)
  216. end
  217. if allowed
  218. true
  219. else
  220. if @project && @project.archived?
  221. render_403 :message => :notice_not_authorized_archived_project
  222. else
  223. deny_access
  224. end
  225. end
  226. end
  227. def authorize_course(ctrl = params[:controller], action = params[:action], global = false)
  228. allowed = User.current.allowed_to?({:controller => ctrl, :action => action}, @course || @course, :global => global)
  229. if allowed
  230. true
  231. else
  232. if @course && @course.archived?
  233. render_403 :message => :notice_not_authorized_archived_project
  234. else
  235. deny_access
  236. end
  237. end
  238. end
  239. def authorize_course_global(ctrl = params[:controller], action = params[:action], global = true)
  240. authorize_course(ctrl, action, global)
  241. end
  242. # Authorize the user for the requested action outside a project
  243. def authorize_global(ctrl = params[:controller], action = params[:action], global = true)
  244. authorize(ctrl, action, global)
  245. end
  246. # Find project of id params[:id]
  247. def find_project
  248. @project = Project.find(params[:id])
  249. rescue ActiveRecord::RecordNotFound
  250. render_404
  251. end
  252. # Find project of id params[:project_id]
  253. def find_project_by_project_id
  254. if params[:project_id]
  255. @project = Project.find(params[:project_id])
  256. elsif params[:course_id]
  257. @course = Course.find(params[:course_id])
  258. end
  259. rescue ActiveRecord::RecordNotFound
  260. render_404
  261. end
  262. def find_contest_by_contest_id
  263. @contest = Contest.find(params[:contest_id])
  264. rescue ActiveRecord::RecordNotFound
  265. render_404
  266. end
  267. #¸ù¾Ýcourse_idÕÒproject
  268. def find_project_by_course_id
  269. @bid = Bid.find params[:course_id]
  270. @project = @bid.courses[0]
  271. rescue ActiveRecord::RecordNotFound
  272. render_404
  273. end
  274. # Find a project based on params[:project_id]
  275. # TODO: some subclasses override this, see about merging their logic
  276. def find_optional_project
  277. @project = Project.find(params[:project_id]) unless params[:project_id].blank?
  278. allowed = User.current.allowed_to?({:controller => params[:controller], :action => params[:action]}, @project, :global => true)
  279. allowed ? true : deny_access
  280. rescue ActiveRecord::RecordNotFound
  281. render_404
  282. end
  283. def find_optional_contest
  284. @contest = Contest.find(params[:contest_id]) unless params[:contest_id].blank?
  285. allowed = User.current.allowed_to?({:controller => params[:controller], :action => params[:action]}, @contest, :global => true)
  286. allowed ? true : deny_access
  287. rescue ActiveRecord::RecordNotFound
  288. render_404
  289. end
  290. # Finds and sets @project based on @object.project
  291. def find_project_from_association
  292. render_404 unless @object.present?
  293. @project = @object.project
  294. if @project == nil && @object.has_attribute?('course_id')
  295. @course = @object.course
  296. end
  297. end
  298. def find_contest_from_association
  299. render_404 unless @object.present?
  300. @contest =@object.contest
  301. end
  302. def find_model_object
  303. model = self.class.model_object
  304. if model
  305. @object = model.find(params[:id])
  306. self.instance_variable_set('@' + controller_name.singularize, @object) if @object
  307. end
  308. rescue ActiveRecord::RecordNotFound
  309. render_404
  310. end
  311. #added by nwb
  312. #»ñȡ¿γÌ
  313. def find_course
  314. @course= Course.find(params[:id])
  315. rescue ActiveRecord::RecordNotFound
  316. render_404
  317. end
  318. # def find_model_object_contest
  319. # model = self.class.model_object
  320. # if model
  321. # @object = model.find(params[:id])
  322. # self.instance_variable_set('@' + controller_name.singularize, @object) if @object
  323. # end
  324. # rescue ActiveRecord::RecordNotFound
  325. # render_404
  326. # end
  327. def self.model_object(model)
  328. self.model_object = model
  329. end
  330. # Find the issue whose id is the :id parameter
  331. # Raises a Unauthorized exception if the issue is not visible
  332. def find_issue
  333. # Issue.visible.find(...) can not be used to redirect user to the login form
  334. # if the issue actually exists but requires authentication
  335. @issue = Issue.find(params[:id])
  336. raise Unauthorized unless @issue.visible?
  337. @project = @issue.project
  338. rescue ActiveRecord::RecordNotFound
  339. render_404
  340. end
  341. # Find issues with a single :id param or :ids array param
  342. # Raises a Unauthorized exception if one of the issues is not visible
  343. def find_issues
  344. @issues = Issue.find_all_by_id(params[:id] || params[:ids])
  345. raise ActiveRecord::RecordNotFound if @issues.empty?
  346. raise Unauthorized unless @issues.all?(&:visible?)
  347. @projects = @issues.collect(&:project).compact.uniq
  348. @project = @projects.first if @projects.size == 1
  349. rescue ActiveRecord::RecordNotFound
  350. render_404
  351. end
  352. def find_attachments
  353. if (attachments = params[:attachments]).present?
  354. att = attachments.values.collect do |attachment|
  355. Attachment.find_by_token( attachment[:token] ) if attachment[:token].present?
  356. end
  357. att.compact!
  358. end
  359. @attachments = att || []
  360. end
  361. # make sure that the user is a member of the project (or admin) if project is private
  362. # used as a before_filter for actions that do not require any particular permission on the project
  363. def check_project_privacy
  364. if @project && !@project.archived?
  365. if @project.visible?
  366. true
  367. else
  368. deny_access
  369. end
  370. else
  371. @project = nil
  372. render_404
  373. false
  374. end
  375. end
  376. def back_url
  377. url = params[:back_url]
  378. if url.nil? && referer = request.env['HTTP_REFERER']
  379. url = CGI.unescape(referer.to_s)
  380. end
  381. url
  382. end
  383. def redirect_back_or_default(default, options={})
  384. back_url = params[:back_url].to_s
  385. if back_url.present?
  386. begin
  387. uri = URI.parse(back_url)
  388. # do not redirect user to another host or to the login or register page
  389. if (uri.relative? || (uri.host == request.host)) && !uri.path.match(%r{/(login|account/register)})
  390. redirect_to(back_url)
  391. return
  392. end
  393. rescue URI::InvalidURIError
  394. logger.warn("Could not redirect to invalid URL #{back_url}")
  395. # redirect to default
  396. end
  397. elsif options[:referer]
  398. redirect_to_referer_or default
  399. return
  400. end
  401. redirect_to default
  402. false
  403. end
  404. # Redirects to the request referer if present, redirects to args or call block otherwise.
  405. def redirect_to_referer_or(*args, &block)
  406. redirect_to :back
  407. rescue ::ActionController::RedirectBackError
  408. if args.any?
  409. redirect_to *args
  410. elsif block_given?
  411. block.call
  412. else
  413. raise "#redirect_to_referer_or takes arguments or a block"
  414. end
  415. end
  416. def render_403(options={})
  417. @project = nil
  418. render_error({:message => :notice_not_authorized, :status => 403}.merge(options))
  419. return false
  420. end
  421. def render_404(options={})
  422. render_error({:message => :notice_file_not_found, :status => 404}.merge(options))
  423. return false
  424. end
  425. # Renders an error response
  426. def render_error(arg)
  427. arg = {:message => arg} unless arg.is_a?(Hash)
  428. @message = arg[:message]
  429. @message = l(@message) if @message.is_a?(Symbol)
  430. @status = arg[:status] || 500
  431. respond_to do |format|
  432. format.html {
  433. render :template => 'common/error', :layout => use_layout, :status => @status
  434. }
  435. format.any { head @status }
  436. end
  437. end
  438. # Handler for ActionView::MissingTemplate exception
  439. def missing_template
  440. logger.warn "Missing template, responding with 404"
  441. @project = nil
  442. render_404
  443. end
  444. # Filter for actions that provide an API response
  445. # but have no HTML representation for non admin users
  446. def require_admin_or_api_request
  447. return true if api_request?
  448. if User.current.admin?
  449. true
  450. elsif User.current.logged?
  451. render_error(:status => 406)
  452. else
  453. deny_access
  454. end
  455. end
  456. # Picks which layout to use based on the request
  457. #
  458. # @return [boolean, string] name of the layout to use or false for no layout
  459. def use_layout
  460. request.xhr? ? false : 'base'
  461. end
  462. def invalid_authenticity_token
  463. if api_request?
  464. logger.error "Form authenticity token is missing or is invalid. API calls must include a proper Content-type header (text/xml or text/json)."
  465. end
  466. render_error "Invalid form authenticity token."
  467. end
  468. def render_feed(items, options={})
  469. @items = items || []
  470. @items.sort! {|x,y| y.event_datetime <=> x.event_datetime }
  471. @items = @items.slice(0, Setting.feeds_limit.to_i)
  472. @title = options[:title] || Setting.app_title
  473. render :template => "common/feed", :formats => [:atom], :layout => false,
  474. :content_type => 'application/atom+xml'
  475. end
  476. def self.accept_rss_auth(*actions)
  477. if actions.any?
  478. self.accept_rss_auth_actions = actions
  479. else
  480. self.accept_rss_auth_actions || []
  481. end
  482. end
  483. def accept_rss_auth?(action=action_name)
  484. self.class.accept_rss_auth.include?(action.to_sym)
  485. end
  486. def self.accept_api_auth(*actions)
  487. if actions.any?
  488. self.accept_api_auth_actions = actions
  489. else
  490. self.accept_api_auth_actions || []
  491. end
  492. end
  493. def accept_api_auth?(action=action_name)
  494. self.class.accept_api_auth.include?(action.to_sym)
  495. end
  496. # Returns the number of objects that should be displayed
  497. # on the paginated list
  498. def per_page_option
  499. per_page = nil
  500. if params[:per_page] && Setting.per_page_options_array.include?(params[:per_page].to_s.to_i)
  501. per_page = params[:per_page].to_s.to_i
  502. session[:per_page] = per_page
  503. elsif session[:per_page]
  504. per_page = session[:per_page]
  505. else
  506. per_page = Setting.per_page_options_array.first || 25
  507. end
  508. per_page
  509. end
  510. # Returns offset and limit used to retrieve objects
  511. # for an API response based on offset, limit and page parameters
  512. def api_offset_and_limit(options=params)
  513. if options[:offset].present?
  514. offset = options[:offset].to_i
  515. if offset < 0
  516. offset = 0
  517. end
  518. end
  519. limit = options[:limit].to_i
  520. if limit < 1
  521. limit = 25
  522. elsif limit > 100
  523. limit = 100
  524. end
  525. if offset.nil? && options[:page].present?
  526. offset = (options[:page].to_i - 1) * limit
  527. offset = 0 if offset < 0
  528. offset ||= 0
  529. end
  530. [offset, limit]
  531. end
  532. # qvalues http header parser
  533. # code taken from webrick
  534. def parse_qvalues(value)
  535. tmp = []
  536. if value
  537. parts = value.split(/,\s*/)
  538. parts.each {|part|
  539. if m = %r{^([^\s,]+?)(?:;\s*q=(\d+(?:\.\d+)?))?$}.match(part)
  540. val = m[1]
  541. q = (m[2] or 1).to_f
  542. tmp.push([val, q])
  543. end
  544. }
  545. tmp = tmp.sort_by{|val, q| -q}
  546. tmp.collect!{|val, q| val}
  547. end
  548. return tmp
  549. rescue
  550. nil
  551. end
  552. # Returns a string that can be used as filename value in Content-Disposition header
  553. def filename_for_content_disposition(name)
  554. request.env['HTTP_USER_AGENT'] =~ %r{MSIE} ? ERB::Util.url_encode(name) : name
  555. end
  556. def api_request?
  557. %w(xml json).include? params[:format]
  558. end
  559. # Returns the API key present in the request
  560. def api_key_from_request
  561. if params[:key].present?
  562. params[:key].to_s
  563. elsif request.headers["X-Redmine-API-Key"].present?
  564. request.headers["X-Redmine-API-Key"].to_s
  565. end
  566. end
  567. # Returns the API 'switch user' value if present
  568. def api_switch_user_from_request
  569. request.headers["X-Redmine-Switch-User"].to_s.presence
  570. end
  571. # Renders a warning flash if obj has unsaved attachments
  572. def render_attachment_warning_if_needed(obj)
  573. flash[:warning] = l(:warning_attachments_not_saved, obj.unsaved_attachments.size) if obj.unsaved_attachments.present?
  574. end
  575. # Sets the `flash` notice or error based the number of issues that did not save
  576. #
  577. # @param [Array, Issue] issues all of the saved and unsaved Issues
  578. # @param [Array, Integer] unsaved_issue_ids the issue ids that were not saved
  579. def set_flash_from_bulk_issue_save(issues, unsaved_issue_ids)
  580. if unsaved_issue_ids.empty?
  581. flash[:notice] = l(:notice_successful_update) unless issues.empty?
  582. else
  583. flash[:error] = l(:notice_failed_to_save_issues,
  584. :count => unsaved_issue_ids.size,
  585. :total => issues.size,
  586. :ids => '#' + unsaved_issue_ids.join(', #'))
  587. end
  588. end
  589. # Rescues an invalid query statement. Just in case...
  590. def query_statement_invalid(exception)
  591. logger.error "Query::StatementInvalid: #{exception.message}" if logger
  592. session.delete(:query)
  593. sort_clear if respond_to?(:sort_clear)
  594. render_error "An error occurred while executing the query and has been logged. Please report this error to your Redmine administrator."
  595. end
  596. # Renders a 200 response for successfull updates or deletions via the API
  597. def render_api_ok
  598. render_api_head :ok
  599. end
  600. # Renders a head API response
  601. def render_api_head(status)
  602. # #head would return a response body with one space
  603. render :text => '', :status => status, :layout => nil
  604. end
  605. # Renders API response on validation failure
  606. def render_validation_errors(objects)
  607. if objects.is_a?(Array)
  608. @error_messages = objects.map {|object| object.errors.full_messages}.flatten
  609. else
  610. @error_messages = objects.errors.full_messages
  611. end
  612. render :template => 'common/error_messages.api', :status => :unprocessable_entity, :layout => nil
  613. end
  614. # Overrides #_include_layout? so that #render with no arguments
  615. # doesn't use the layout for api requests
  616. def _include_layout?(*args)
  617. api_request? ? false : super
  618. end
  619. ## Please added code in html.
  620. # <div class="pagination"><%= pagination_links_full @obj_pages, @obj_count, :per_page_links => false %></div>
  621. def paginateHelper obj, pre_size=20
  622. @obj_count = obj.count
  623. @obj_pages = Paginator.new @obj_count, pre_size, params['page']
  624. if obj.kind_of? ActiveRecord::Base or obj.kind_of? ActiveRecord::Relation
  625. obj.limit(@obj_pages.per_page).offset(@obj_pages.offset)
  626. elsif obj.kind_of? Array
  627. obj[@obj_pages.offset, @obj_pages.per_page]
  628. else
  629. logger.error "[ApplicationController] Error : application_controller#paginateHelper ===> unknow category: #{obj.class}"
  630. raise RuntimeError, 'unknow type, Please input you type into this helper.'
  631. end
  632. end
  633. end

软件技术创新是当今国家整体创新能力的重要基础和发展引擎。近年来,大众参与的软件创新与创业活动已经成为网络时代软件开发和应用的新形态,正在快速改变着全球软件创新模式和软件产业格局。 ...