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 31 kB

13 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
13 years ago
​
13 years ago
13 years ago
​
​
​
​
​
​
​
​
​
13 years ago
​
​
​
​
​
​
​
​
​
​
​
​
13 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
12 years ago
13 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
10 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
​
​
​
​
​
​
​
​
​
​
13 years ago
13 years ago
​
​
​
​
​
​
12 years ago
​
​
​
​
​
​
13 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
13 years ago
​
​
​
​
​
​
​
13 years ago
​
12 years ago
12 years ago
13 years ago
​
​
​
​
​
​
​
​
​
​
12 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
10 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
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
13 years ago
​
​
​
​
​
​
13 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
13 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
13 years ago
​
​
13 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
13 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
12 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
13 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997
  1. # encoding: utf-8
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2013 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. require 'uri'
  19. require 'cgi'
  20. class Unauthorized < Exception; end
  21. class ApplicationController < ActionController::Base
  22. include Redmine::I18n
  23. include Redmine::Pagination
  24. include RoutesHelper
  25. helper :routes
  26. #added by william
  27. helper :watchers
  28. class_attribute :accept_api_auth_actions
  29. class_attribute :accept_rss_auth_actions
  30. class_attribute :model_object
  31. layout 'base'
  32. protect_from_forgery
  33. def handle_unverified_request
  34. super
  35. raise(ActionController::InvalidAuthenticityToken)
  36. # cookies.delete(autologin_cookie_name)
  37. end
  38. before_filter :find_first_page
  39. before_filter :find_web_footer
  40. before_filter :session_expiration, :user_setup, :check_if_login_required, :set_localization
  41. before_filter :user_agent
  42. rescue_from ActionController::InvalidAuthenticityToken, :with => :invalid_authenticity_token
  43. rescue_from ::Unauthorized, :with => :deny_access
  44. rescue_from ::ActionView::MissingTemplate, :with => :missing_template
  45. include Redmine::Search::Controller
  46. include Redmine::MenuManager::MenuController
  47. helper Redmine::MenuManager::MenuHelper
  48. def user_agent
  49. logger.info "HTTP_USER_AGENT #{request.env["HTTP_USER_AGENT"]}"
  50. end
  51. def session_expiration
  52. if session[:user_id]
  53. if session_expired? && !try_to_autologin
  54. reset_session
  55. flash[:error] = l(:error_session_expired)
  56. redirect_to signin_url
  57. else
  58. session[:atime] = Time.now.utc.to_i
  59. end
  60. end
  61. end
  62. def session_expired?
  63. if Setting.session_lifetime?
  64. unless session[:ctime] && (Time.now.utc.to_i - session[:ctime].to_i <= Setting.session_lifetime.to_i * 60)
  65. return true
  66. end
  67. end
  68. if Setting.session_timeout?
  69. unless session[:atime] && (Time.now.utc.to_i - session[:atime].to_i <= Setting.session_timeout.to_i * 60)
  70. return true
  71. end
  72. end
  73. false
  74. end
  75. def start_user_session(user)
  76. session[:user_id] = user.id
  77. session[:ctime] = Time.now.utc.to_i
  78. session[:atime] = Time.now.utc.to_i
  79. end
  80. def user_setup
  81. # Check the settings cache for each request
  82. Setting.check_cache
  83. # Find the current user
  84. User.current = find_current_user
  85. #logger.info(" Current user: " + (User.current.logged? ? "#{User.current.login} (id=#{User.current.id})" : "anonymous")) if logger
  86. end
  87. # Returns the current user or nil if no user is logged in
  88. # and starts a session if needed
  89. def current_user
  90. find_current_user
  91. end
  92. def find_current_user
  93. user = nil
  94. unless api_request?
  95. if session[:user_id]
  96. # existing session
  97. user = (User.active.find(session[:user_id]) rescue nil)
  98. elsif autologin_user = try_to_autologin
  99. user = autologin_user
  100. elsif params[:format] == 'atom' && params[:key] && request.get? && accept_rss_auth?
  101. # RSS key authentication does not start a session
  102. user = User.find_by_rss_key(params[:key])
  103. elsif session[:wechat_openid]
  104. uw = UserWechat.find_by_openid(session[:wechat_openid])
  105. user = uw.user if uw
  106. end
  107. end
  108. if user.nil? && Setting.rest_api_enabled? && accept_api_auth?
  109. if (key = api_key_from_request)
  110. # Use API key
  111. user = User.find_by_api_key(key)
  112. else
  113. # HTTP Basic, either username/password or API key/random
  114. authenticate_with_http_basic do |username, password|
  115. user = User.try_to_login(username, password)[0] || User.find_by_api_key(username)
  116. end
  117. end
  118. # Switch user if requested by an admin user
  119. if user && user.admin? && (username = api_switch_user_from_request)
  120. su = User.find_by_login(username)
  121. if su && su.active?
  122. logger.info(" User switched by: #{user.login} (id=#{user.id})") if logger
  123. user = su
  124. else
  125. render_error :message => 'Invalid X-Redmine-Switch-User header', :status => 412
  126. end
  127. end
  128. end
  129. user
  130. end
  131. def autologin_cookie_name
  132. Redmine::Configuration['autologin_cookie_name'].presence || 'autologin'
  133. end
  134. def try_to_autologin
  135. if cookies[autologin_cookie_name] && Setting.autologin?
  136. # auto-login feature starts a new session
  137. user = User.try_to_autologin(cookies[autologin_cookie_name])
  138. if user
  139. reset_session
  140. start_user_session(user)
  141. end
  142. user
  143. end
  144. end
  145. def try_to_autologin1
  146. user = User.try_to_autologin(params[:token])
  147. if user
  148. logout_user if User.current.id != user.id
  149. start_user_session(user)
  150. end
  151. user
  152. end
  153. # Sets the logged in user
  154. def logged_user=(user)
  155. reset_session
  156. if user && user.is_a?(User)
  157. User.current = user
  158. start_user_session(user)
  159. else
  160. User.current = User.anonymous
  161. end
  162. end
  163. def logged_user_by_apptoken
  164. #从手机端传来apptoken则将当前登陆用户变为对应的用户
  165. if params[:apptoken]
  166. token = ApiKey.where(access_token: params[:apptoken]).first
  167. if token.expired?
  168. return
  169. end
  170. if token && !token.expired?
  171. @current_user = User.find(token.user_id)
  172. end
  173. unless @current_user.nil?
  174. self.logged_user = @current_user
  175. if @current_user
  176. @current_user.update_column(:last_login_on, Time.now)
  177. end
  178. end
  179. end
  180. end
  181. # Logs out current user
  182. def logout_user
  183. if User.current.logged?
  184. if Redmine::Configuration['cookie_domain'].present?
  185. cookies.delete(autologin_cookie_name, domain: Redmine::Configuration['cookie_domain'])
  186. else
  187. cookies.delete autologin_cookie_name
  188. end
  189. # Token.delete_all(["user_id = ? AND action = ?", User.current.id, 'autologin'])
  190. self.logged_user = nil
  191. end
  192. end
  193. # check if login is globally required to access the application
  194. def check_if_login_required
  195. # no check needed if user is already logged in
  196. return true if User.current.logged?
  197. require_login if Setting.login_required?
  198. end
  199. def set_localization
  200. lang = nil
  201. if User.current.logged?
  202. lang = find_language(User.current.language)
  203. end
  204. if lang.nil? && request.env['HTTP_ACCEPT_LANGUAGE']
  205. accept_lang = parse_qvalues(request.env['HTTP_ACCEPT_LANGUAGE']).first
  206. if !accept_lang.blank?
  207. accept_lang = accept_lang.downcase
  208. lang = find_language(accept_lang) || find_language(accept_lang.split('-').first)
  209. end
  210. end
  211. lang ||= Setting.default_language
  212. set_language_if_valid(lang)
  213. end
  214. def require_login
  215. if !User.current.logged?
  216. # Extract only the basic url parameters on non-GET requests
  217. if request.get?
  218. url = url_for(params)
  219. else
  220. url = url_for(:controller => params[:controller], :action => params[:action], :id => params[:id], :project_id => params[:project_id])
  221. end
  222. respond_to do |format|
  223. format.html { redirect_to signin_url }
  224. format.atom { redirect_to signin_url }
  225. format.xml { head :unauthorized, 'WWW-Authenticate' => 'Basic realm="Redmine API"' }
  226. format.js { head :unauthorized, 'WWW-Authenticate' => 'Basic realm="Redmine API"' }
  227. format.json { head :unauthorized, 'WWW-Authenticate' => 'Basic realm="Redmine API"' }
  228. end
  229. return false
  230. end
  231. true
  232. end
  233. def require_admin
  234. return unless require_login
  235. if !User.current.admin?
  236. render_403
  237. return false
  238. end
  239. true
  240. end
  241. def deny_access
  242. User.current.logged? ? render_403 : require_login
  243. end
  244. # Authorize the user for the requested action
  245. def authorize(ctrl = params[:controller], action = params[:action], global = false)
  246. #modify by NWB
  247. allowed = authorize_allowed(params[:controller], params[:action],global)
  248. if allowed
  249. true
  250. else
  251. if @project && @project.archived?
  252. render_403 :message => :notice_not_authorized_archived_project
  253. else
  254. deny_access
  255. end
  256. end
  257. end
  258. def authorize1(ctrl = params[:controller], action = params[:action],token = params[:token], global = false)
  259. if(!User.current.logged? && !token.nil?)
  260. User.current =try_to_autologin1
  261. end
  262. allowed = authorize_allowed(params[:controller], params[:action],global)
  263. if allowed
  264. true
  265. else
  266. if @project && @project.archived?
  267. render_403 :message => :notice_not_authorized_archived_project
  268. else
  269. deny_access
  270. end
  271. end
  272. end
  273. def auth_login1(token = params[:token])
  274. if(!User.current.logged? && !token.nil?)
  275. User.current =try_to_autologin1
  276. end
  277. end
  278. def authorize_allowed(ctrl = params[:controller], action = params[:action], global = false)
  279. #modify by NWB
  280. if @project
  281. if ctrl == "repositories" && action == "show" && @project.hidden_repo
  282. allowed = User.current.admin? || (User.current.member_of?(@project) && !(role_of_members_in_project(@project.id, User.current.id) == "Reporter"))
  283. else
  284. allowed = User.current.allowed_to?({:controller => ctrl, :action => action}, @project || @projects, :global => global)
  285. end
  286. elsif @course
  287. allowed = User.current.allowed_to?({:controller => ctrl, :action => action}, @course || @courses, :global => global)
  288. else
  289. # allowed = User.current.allowed_to?({:controller => ctrl, :action => action}, @project || @projects, :global => global)
  290. allowed = true
  291. end
  292. allowed
  293. end
  294. def authorize_attachment_download(ctrl = params[:controller], action = params[:action], global = false)
  295. case @attachment.container_type
  296. when "Memo"
  297. allowed = User.current.allowed_to?(:memos_attachments_download,nil,:global => true)
  298. when "Message"
  299. if @project
  300. allowed = User.current.allowed_to?(:projects_attachments_download,@project,:global => false)
  301. elsif @course
  302. allowed = User.current.allowed_to?(:course_attachments_download, @course, :global => false)
  303. end
  304. when "contest"
  305. return true
  306. when "Course"
  307. allowed = User.current.allowed_to?(:course_attachments_download, @course, :global => false)
  308. else
  309. return true
  310. end
  311. if allowed
  312. true
  313. else
  314. if @project && @project.archived?
  315. render_403 :message => :notice_not_authorized_archived_project
  316. else
  317. deny_access
  318. end
  319. end
  320. end
  321. def authorize_attachment_download1(ctrl = params[:controller], action = params[:action],token = params[:token], global = false)
  322. if(!User.current.logged? && !token.nil?)
  323. User.current = try_to_autologin1
  324. end
  325. case @attachment.container_type
  326. when "Memo"
  327. allowed = User.current.allowed_to?(:memos_attachments_download,nil,:global => true)
  328. when "Message"
  329. if @project
  330. allowed = User.current.allowed_to?(:projects_attachments_download,@project,:global => false)
  331. elsif @course
  332. allowed = User.current.allowed_to?(:course_attachments_download, @course, :global => false)
  333. elsif @contest
  334. allowed = User.current.member_of_contest?(@contest)
  335. end
  336. when "contest"
  337. return true
  338. when "Course"
  339. if @attachment.get_status_by_attach(User.current.id) == 2
  340. return true
  341. else
  342. allowed = syllabus_course_member(User.current, @course) || @attachment.is_public == 1
  343. end
  344. else
  345. return true
  346. end
  347. if allowed || (params[:type] && params[:type] == "wechat" )
  348. true
  349. else
  350. if @project && @project.archived?
  351. render_403 :message => :notice_not_authorized_archived_project
  352. else
  353. deny_access
  354. end
  355. end
  356. end
  357. def authorize_course(ctrl = params[:controller], action = params[:action], global = false)
  358. allowed = User.current.allowed_to?({:controller => ctrl, :action => action}, @course || @course, :global => global)
  359. if allowed
  360. true
  361. else
  362. if params[:action] == 'show'
  363. #更新申请结果反馈消息的状态
  364. messages = CourseMessage.where("course_message_type =? and course_id =? and user_id =? and viewed =?", 'CourseRequestDealResult', @course.id, User.current.id, false)
  365. messages.update_all(:viewed => true)
  366. end
  367. if @course && @course.archived?
  368. render_403 :message => :notice_not_authorized_archived_project
  369. else
  370. deny_access
  371. end
  372. end
  373. end
  374. def authorize_course_global(ctrl = params[:controller], action = params[:action], global = true)
  375. authorize_course(ctrl, action, global)
  376. end
  377. # Authorize the user for the requested action outside a project
  378. def authorize_global(ctrl = params[:controller], action = params[:action], global = true)
  379. authorize(ctrl, action, global)
  380. end
  381. # Find project of id params[:id]
  382. def find_project
  383. @project = Project.find(params[:id])
  384. rescue ActiveRecord::RecordNotFound
  385. render_404
  386. end
  387. # Find project of id params[:project_id]
  388. def find_project_by_project_id
  389. if params[:project_id]
  390. @project = Project.find(params[:project_id])
  391. elsif params[:course_id]
  392. @course = Course.find(params[:course_id])
  393. elsif params[:org_subfield_id]
  394. @org_subfield = OrgSubfield.find(params[:org_subfield_id])
  395. elsif params[:contest_id]
  396. @contest = Contest.find(params[:contest_id])
  397. end
  398. rescue ActiveRecord::RecordNotFound
  399. render_404
  400. end
  401. def find_contest_by_contest_id
  402. @contest = Contest.find(params[:contest_id])
  403. rescue ActiveRecord::RecordNotFound
  404. render_404
  405. end
  406. #���course_id��project
  407. def find_project_by_course_id
  408. @bid = Bid.find params[:course_id]
  409. @project = @bid.courses[0]
  410. rescue ActiveRecord::RecordNotFound
  411. render_404
  412. end
  413. # Find a project based on params[:project_id]
  414. # TODO: some subclasses override this, see about merging their logic
  415. def find_optional_project
  416. @project = Project.find(params[:project_id]) unless params[:project_id].blank?
  417. allowed = User.current.allowed_to?({:controller => params[:controller], :action => params[:action]}, @project, :global => true)
  418. allowed ? true : deny_access
  419. rescue ActiveRecord::RecordNotFound
  420. render_404
  421. end
  422. def find_optional_contest
  423. @contest = Contest.find(params[:contest_id]) unless params[:contest_id].blank?
  424. allowed = User.current.allowed_to?({:controller => params[:controller], :action => params[:action]}, @contest, :global => true)
  425. allowed ? true : deny_access
  426. rescue ActiveRecord::RecordNotFound
  427. render_404
  428. end
  429. # Finds and sets @project based on @object.project
  430. def find_project_from_association
  431. render_404 unless @object.present?
  432. @project = @object.project
  433. if @project == nil && @object.has_attribute?('course_id')
  434. @course = @object.course
  435. if @course.nil? && @object.has_attribute?('contest_id')
  436. @contest = @object.contest
  437. end
  438. end
  439. end
  440. def find_contest_from_association
  441. render_404 unless @object.present?
  442. @contest =@object.contest
  443. end
  444. def find_model_object
  445. model = self.class.model_object
  446. if model
  447. @object = model.find(params[:id])
  448. self.instance_variable_set('@' + controller_name.singularize, @object) if @object
  449. end
  450. rescue ActiveRecord::RecordNotFound
  451. render_404
  452. end
  453. #added by nwb
  454. #��ȡ�γ�
  455. def find_course
  456. @course= Course.find(params[:id])
  457. rescue ActiveRecord::RecordNotFound
  458. render_404
  459. end
  460. # def find_model_object_contest
  461. # model = self.class.model_object
  462. # if model
  463. # @object = model.find(params[:id])
  464. # self.instance_variable_set('@' + controller_name.singularize, @object) if @object
  465. # end
  466. # rescue ActiveRecord::RecordNotFound
  467. # render_404
  468. # end
  469. def self.model_object(model)
  470. self.model_object = model
  471. end
  472. # Find the issue whose id is the :id parameter
  473. # Raises a Unauthorized exception if the issue is not visible
  474. def find_issue
  475. # Issue.visible.find(...) can not be used to redirect user to the login form
  476. # if the issue actually exists but requires authentication
  477. @issue = Issue.find(params[:id])
  478. raise Unauthorized unless @issue.visible?
  479. @project = @issue.project
  480. rescue ActiveRecord::RecordNotFound
  481. render_404
  482. end
  483. # Find issues with a single :id param or :ids array param
  484. # Raises a Unauthorized exception if one of the issues is not visible
  485. def find_issues
  486. @issues = Issue.find_all_by_id(params[:id] || params[:ids])
  487. raise ActiveRecord::RecordNotFound if @issues.empty?
  488. raise Unauthorized unless @issues.all?(&:visible?)
  489. @projects = @issues.collect(&:project).compact.uniq
  490. @project = @projects.first if @projects.size == 1
  491. rescue ActiveRecord::RecordNotFound
  492. render_404
  493. end
  494. def find_attachments
  495. if (attachments = params[:attachments]).present?
  496. att = attachments.values.collect do |attachment|
  497. Attachment.find_by_token( attachment[:token] ) if attachment[:token].present?
  498. end
  499. att.compact!
  500. end
  501. @attachments = att || []
  502. end
  503. # make sure that the user is a member of the project (or admin) if project is private
  504. # used as a before_filter for actions that do not require any particular permission on the project
  505. def check_project_privacy
  506. if @project && !@project.archived?
  507. if @project.visible?
  508. true
  509. else
  510. deny_access
  511. end
  512. else
  513. @project = nil
  514. render_404
  515. false
  516. end
  517. end
  518. def back_url
  519. url = params[:back_url]
  520. if url.nil? && referer = request.env['HTTP_REFERER']
  521. url = CGI.unescape(referer.to_s)
  522. end
  523. url
  524. end
  525. def redirect_back_or_default(default, options={})
  526. back_url = ''#params[:back_url].to_s
  527. if back_url.present?
  528. begin
  529. uri = URI.parse(back_url)
  530. # do not redirect user to another host or to the login or register page
  531. if (uri.relative? || (uri.host == request.host)) && !uri.path.match(%r{/(login|account/register)})
  532. back_url = back_url.gsub(%r{\/users\/(\w+)},"/users/"+default.to_s) if default.is_a?(:User)
  533. redirect_to(back_url)
  534. return
  535. end
  536. rescue URI::InvalidURIError
  537. logger.warn("Could not redirect to invalid URL #{back_url}")
  538. #redirect to default
  539. end
  540. elsif options[:referer]
  541. redirect_to_referer_or default
  542. return
  543. end
  544. redirect_to default
  545. false
  546. end
  547. # Redirects to the request referer if present, redirects to args or call block otherwise.
  548. def redirect_to_referer_or(*args, &block)
  549. redirect_to :back
  550. rescue ::ActionController::RedirectBackError
  551. if args.any?
  552. redirect_to *args
  553. elsif block_given?
  554. block.call
  555. else
  556. raise "#redirect_to_referer_or takes arguments or a block"
  557. end
  558. end
  559. def render_403(options={})
  560. @project = nil
  561. render_error({:message => :notice_not_authorized, :status => 403}.merge(options),'common/403')
  562. #render :template => 'common/403'
  563. return false
  564. end
  565. def render_404(options={})
  566. render_error({:message => :notice_file_not_found, :status => 404}.merge(options),'common/404')
  567. #render :template => 'common/404'
  568. return false
  569. end
  570. # Renders an error response
  571. def render_error(arg,template = 'common/error')
  572. arg = {:message => arg} unless arg.is_a?(Hash)
  573. @message = arg[:message]
  574. @lay = arg[:layout]
  575. @message = l(@message) if @message.is_a?(Symbol)
  576. @status = arg[:status] || 500
  577. respond_to do |format|
  578. format.html {
  579. if @lay
  580. render :template => template, :layout => @lay,:status => @status
  581. else
  582. render :template => template, :layout => use_layout, :status => @status
  583. end
  584. }
  585. format.any { head @status }
  586. end
  587. end
  588. # Handler for ActionView::MissingTemplate exception
  589. def missing_template
  590. logger.warn "Missing template, responding with 404"
  591. @project = nil
  592. render_404
  593. end
  594. # Filter for actions that provide an API response
  595. # but have no HTML representation for non admin users
  596. def require_admin_or_api_request
  597. return true if api_request?
  598. if User.current.admin?
  599. true
  600. elsif User.current.logged?
  601. render_error(:status => 406)
  602. else
  603. deny_access
  604. end
  605. end
  606. # Picks which layout to use based on the request
  607. #
  608. # @return [boolean, string] name of the layout to use or false for no layout
  609. def use_layout
  610. request.xhr? ? false : 'users_base'
  611. end
  612. def invalid_authenticity_token
  613. if api_request?
  614. logger.error "Form authenticity token is missing or is invalid. API calls must include a proper Content-type header (text/xml or text/json)."
  615. end
  616. #render_error "Invalid form authenticity token."
  617. redirect_to signin_path(:login=>true)
  618. end
  619. def render_feed(items, options={})
  620. @items = items || []
  621. @items.sort! {|x,y| y.event_datetime <=> x.event_datetime }
  622. @items = @items.slice(0, Setting.feeds_limit.to_i)
  623. @title = options[:title] || Setting.app_title
  624. render :template => "common/feed", :formats => [:atom], :layout => false,
  625. :content_type => 'application/atom+xml'
  626. end
  627. def issue_ratio_change done_ratio, status_id
  628. if done_ratio == 100 || status_id == 3
  629. "已完成"
  630. else
  631. done_ratio.to_s + "%"
  632. end
  633. end
  634. def issue_priority_change priority_id
  635. case priority_id
  636. when 1
  637. "低"
  638. when 2
  639. "正常"
  640. when 3
  641. "高"
  642. when 4
  643. "紧急"
  644. when 5
  645. "立即"
  646. end
  647. end
  648. def issue_status_change status_id
  649. case status_id
  650. when 1
  651. "新增"
  652. when 2
  653. "正在解决"
  654. when 3
  655. "已解决"
  656. when 4
  657. "反馈"
  658. when 5
  659. "关闭"
  660. when 6
  661. "拒绝"
  662. end
  663. end
  664. def issue_tracker_change tracker_id
  665. case tracker_id
  666. when 1
  667. "缺陷"
  668. when 2
  669. "功能"
  670. when 3
  671. "支持"
  672. when 4
  673. "任务"
  674. when 5
  675. "周报"
  676. end
  677. end
  678. def self.accept_rss_auth(*actions)
  679. if actions.any?
  680. self.accept_rss_auth_actions = actions
  681. else
  682. self.accept_rss_auth_actions || []
  683. end
  684. end
  685. def accept_rss_auth?(action=action_name)
  686. self.class.accept_rss_auth.include?(action.to_sym)
  687. end
  688. def self.accept_api_auth(*actions)
  689. if actions.any?
  690. self.accept_api_auth_actions = actions
  691. else
  692. self.accept_api_auth_actions || []
  693. end
  694. end
  695. def accept_api_auth?(action=action_name)
  696. self.class.accept_api_auth.include?(action.to_sym)
  697. end
  698. # Returns the number of objects that should be displayed
  699. # on the paginated list
  700. def per_page_option
  701. per_page = nil
  702. if params[:per_page] && Setting.per_page_options_array.include?(params[:per_page].to_s.to_i)
  703. per_page = params[:per_page].to_s.to_i
  704. session[:per_page] = per_page
  705. elsif session[:per_page]
  706. per_page = session[:per_page]
  707. else
  708. per_page = Setting.per_page_options_array.first || 25
  709. end
  710. per_page
  711. end
  712. # Returns offset and limit used to retrieve objects
  713. # for an API response based on offset, limit and page parameters
  714. def api_offset_and_limit(options=params)
  715. if options[:offset].present?
  716. offset = options[:offset].to_i
  717. if offset < 0
  718. offset = 0
  719. end
  720. end
  721. limit = options[:limit].to_i
  722. if limit < 1
  723. limit = 25
  724. elsif limit > 100
  725. limit = 100
  726. end
  727. if offset.nil? && options[:page].present?
  728. offset = (options[:page].to_i - 1) * limit
  729. offset = 0 if offset < 0
  730. offset ||= 0
  731. end
  732. [offset, limit]
  733. end
  734. # qvalues http header parser
  735. # code taken from webrick
  736. def parse_qvalues(value)
  737. tmp = []
  738. if value
  739. parts = value.split(/,\s*/)
  740. parts.each {|part|
  741. if m = %r{^([^\s,]+?)(?:;\s*q=(\d+(?:\.\d+)?))?$}.match(part)
  742. val = m[1]
  743. q = (m[2] or 1).to_f
  744. tmp.push([val, q])
  745. end
  746. }
  747. tmp = tmp.sort_by{|val, q| -q}
  748. tmp.collect!{|val, q| val}
  749. end
  750. return tmp
  751. rescue
  752. nil
  753. end
  754. # Returns a string that can be used as filename value in Content-Disposition header
  755. def filename_for_content_disposition(name)
  756. request.env['HTTP_USER_AGENT'] =~ %r{MSIE|Trident|Edge} ? ERB::Util.url_encode(name) : name
  757. end
  758. def api_request?
  759. return false if params[:controller] == 'at'
  760. %w(xml json).include? params[:format]
  761. end
  762. # Returns the API key present in the request
  763. def api_key_from_request
  764. if params[:key].present?
  765. params[:key].to_s
  766. elsif request.headers["X-Redmine-API-Key"].present?
  767. request.headers["X-Redmine-API-Key"].to_s
  768. end
  769. end
  770. # Returns the API 'switch user' value if present
  771. def api_switch_user_from_request
  772. request.headers["X-Redmine-Switch-User"].to_s.presence
  773. end
  774. # Renders a warning flash if obj has unsaved attachments
  775. def render_attachment_warning_if_needed(obj)
  776. flash[:warning] = l(:warning_attachments_not_saved, obj.unsaved_attachments.size) if obj.unsaved_attachments.present?
  777. end
  778. # Sets the `flash` notice or error based the number of issues that did not save
  779. #
  780. # @param [Array, Issue] issues all of the saved and unsaved Issues
  781. # @param [Array, Integer] unsaved_issue_ids the issue ids that were not saved
  782. def set_flash_from_bulk_issue_save(issues, unsaved_issue_ids)
  783. if unsaved_issue_ids.empty?
  784. flash[:notice] = l(:notice_successful_update) unless issues.empty?
  785. else
  786. flash[:error] = l(:notice_failed_to_save_issues,
  787. :count => unsaved_issue_ids.size,
  788. :total => issues.size,
  789. :ids => '#' + unsaved_issue_ids.join(', #'))
  790. end
  791. end
  792. # Rescues an invalid query statement. Just in case...
  793. def query_statement_invalid(exception)
  794. logger.error "Query::StatementInvalid: #{exception.message}" if logger
  795. session.delete(:query)
  796. sort_clear if respond_to?(:sort_clear)
  797. render_error "An error occurred while executing the query and has been logged. Please report this error to your Redmine administrator."
  798. end
  799. # Renders a 200 response for successfull updates or deletions via the API
  800. def render_api_ok
  801. render_api_head :ok
  802. end
  803. # Renders a head API response
  804. def render_api_head(status)
  805. # #head would return a response body with one space
  806. render :text => '', :status => status, :layout => nil
  807. end
  808. # Renders API response on validation failure
  809. def render_validation_errors(objects)
  810. if objects.is_a?(Array)
  811. @error_messages = objects.map {|object| object.errors.full_messages}.flatten
  812. else
  813. @error_messages = objects.errors.full_messages
  814. end
  815. render :template => 'common/error_messages.api', :status => :unprocessable_entity, :layout => nil
  816. end
  817. # Overrides #_include_layout? so that #render with no arguments
  818. # doesn't use the layout for api requests
  819. def _include_layout?(*args)
  820. api_request? ? false : super
  821. end
  822. ## Please added code in html.
  823. # <div class="pagination"><%= pagination_links_full @obj_pages, @obj_count, :per_page_links => false %></div>
  824. def paginateHelper obj, pre_size=20
  825. @obj_count = obj.count
  826. @obj_pages = Paginator.new @obj_count, pre_size, params['page']
  827. if obj.kind_of? ActiveRecord::Base or obj.kind_of? ActiveRecord::Relation
  828. obj.limit(@obj_pages.per_page).offset(@obj_pages.offset)
  829. elsif obj.kind_of? Array
  830. obj[@obj_pages.offset, @obj_pages.per_page]
  831. else
  832. logger.error "[ApplicationController] Error : application_controller#paginateHelper ===> unknow category: #{obj.class}"
  833. raise RuntimeError, 'unknow type, Please input you type into this helper.'
  834. end
  835. end
  836. def paginateHelper_for_members obj, pre_size=10
  837. @obj_count = StudentsForCourse.find_by_sql("select count(id) as mem_count from students_for_courses where course_id = #{@course.id}")[0][:mem_count].to_s.to_i
  838. @obj_pages = Paginator.new @obj_count, pre_size, params['page']
  839. if obj.kind_of? ActiveRecord::Base or obj.kind_of? ActiveRecord::Relation
  840. obj.limit(@obj_pages.per_page).offset(0)
  841. elsif obj.kind_of? Array
  842. obj[0, @obj_pages.per_page]
  843. else
  844. logger.error "[ApplicationController] Error : application_controller#paginateHelper ===> unknow category: #{obj.class}"
  845. raise RuntimeError, 'unknow type, Please input you type into this helper.'
  846. end
  847. end
  848. #查找首页相关信息
  849. def find_first_page
  850. @first_page = FirstPage.find_by_page_type('project')
  851. if @first_page.nil?
  852. @first_page = FirstPage.new
  853. @first_page.page_type = 'project'
  854. @first_page.description = ""
  855. @first_page.save()
  856. end
  857. @show_course = @first_page.show_course
  858. @show_contest = @first_page.show_contest
  859. end
  860. def find_web_footer
  861. @organizer = WebFooterOranizer.first
  862. @companies = WebFooterCompany.all
  863. end
  864. def password_authentication
  865. user, last_login_on = User.try_to_login(params[:user_name], params[:password])
  866. successful_authentication(user, last_login_on)
  867. end
  868. def successful_authentication(user, last_login_on)
  869. logger.info "Successful authentication for '#{user.login}' from #{request.remote_ip} at #{Time.now.utc}"
  870. # Valid user
  871. self.logged_user = user
  872. # generate a key and set cookie if autologin
  873. if params[:autologin] && Setting.autologin?
  874. set_autologin_cookie(user)
  875. end
  876. call_hook(:controller_account_success_authentication_after, {:user => user })
  877. end
  878. def user_unlogged_check
  879. if !User.current.logged?
  880. render(:partial => 'organizations/unlogged_tip')
  881. return false
  882. end
  883. true
  884. end
  885. end

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