|
| 1 | +#-- encoding: UTF-8 |
| 2 | +#-- copyright |
| 3 | +# OpenProject is a project management system. |
| 4 | +# Copyright (C) 2012-2018 the OpenProject Foundation (OPF) |
| 5 | +# |
| 6 | +# This program is free software; you can redistribute it and/or |
| 7 | +# modify it under the terms of the GNU General Public License version 3. |
| 8 | +# |
| 9 | +# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: |
| 10 | +# Copyright (C) 2006-2017 Jean-Philippe Lang |
| 11 | +# Copyright (C) 2010-2013 the ChiliProject Team |
| 12 | +# |
| 13 | +# This program is free software; you can redistribute it and/or |
| 14 | +# modify it under the terms of the GNU General Public License |
| 15 | +# as published by the Free Software Foundation; either version 2 |
| 16 | +# of the License, or (at your option) any later version. |
| 17 | +# |
| 18 | +# This program is distributed in the hope that it will be useful, |
| 19 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | +# GNU General Public License for more details. |
| 22 | +# |
| 23 | +# You should have received a copy of the GNU General Public License |
| 24 | +# along with this program; if not, write to the Free Software |
| 25 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 26 | +# |
| 27 | +# See docs/COPYRIGHT.rdoc for more details. |
| 28 | +#++ |
| 29 | + |
| 30 | + |
| 31 | +module OAuth |
| 32 | + class ApplicationsController < ::ApplicationController |
| 33 | + before_action :require_admin |
| 34 | + before_action :new_app, only: %i[new create] |
| 35 | + before_action :find_app, only: %i[edit update show destroy] |
| 36 | + |
| 37 | + layout 'admin' |
| 38 | + menu_item :oauth_applications |
| 39 | + |
| 40 | + def index |
| 41 | + @applications = ::Doorkeeper::Application.includes(:owner).all |
| 42 | + end |
| 43 | + |
| 44 | + def new; end |
| 45 | + def edit; end |
| 46 | + |
| 47 | + def show |
| 48 | + @reveal_secret = flash[:reveal_secret] |
| 49 | + flash.delete :reveal_secret |
| 50 | + end |
| 51 | + |
| 52 | + def create |
| 53 | + call = ::OAuth::PersistApplicationService.new(@application, user: current_user) |
| 54 | + .call(permitted_params.oauth_application) |
| 55 | + |
| 56 | + if call.success? |
| 57 | + flash[:notice] = t(:notice_successful_create) |
| 58 | + flash[:_application_secret] = call.result.plaintext_secret |
| 59 | + redirect_to action: :show, id: call.result.id |
| 60 | + else |
| 61 | + @errors = call.errors |
| 62 | + flash[:error] = call.errors.full_messages.join('\n') |
| 63 | + render action: :new |
| 64 | + end |
| 65 | + end |
| 66 | + |
| 67 | + def update |
| 68 | + call = ::OAuth::PersistApplicationService.new(@application, user: current_user) |
| 69 | + .call(permitted_params.oauth_application) |
| 70 | + |
| 71 | + if call.success? |
| 72 | + flash[:notice] = t(:notice_successful_update) |
| 73 | + redirect_to action: :index |
| 74 | + else |
| 75 | + @errors = call.errors |
| 76 | + flash[:error] = call.errors.full_messages.join('\n') |
| 77 | + render action: :edit |
| 78 | + end |
| 79 | + end |
| 80 | + |
| 81 | + def destroy |
| 82 | + if @application.destroy |
| 83 | + flash[:notice] = t(:notice_successful_delete) |
| 84 | + else |
| 85 | + flash[:error] = t(:error_can_not_delete_entry) |
| 86 | + end |
| 87 | + |
| 88 | + redirect_to action: :index |
| 89 | + end |
| 90 | + |
| 91 | + |
| 92 | + protected |
| 93 | + |
| 94 | + def default_breadcrumb |
| 95 | + if action_name == 'index' |
| 96 | + t('oauth.application.plural') |
| 97 | + else |
| 98 | + ActionController::Base.helpers.link_to(t('oauth.application.plural'), oauth_applications_path) |
| 99 | + end |
| 100 | + end |
| 101 | + |
| 102 | + def show_local_breadcrumb |
| 103 | + current_user.admin? |
| 104 | + end |
| 105 | + |
| 106 | + private |
| 107 | + |
| 108 | + def new_app |
| 109 | + @application = ::Doorkeeper::Application.new |
| 110 | + end |
| 111 | + |
| 112 | + def find_app |
| 113 | + @application = ::Doorkeeper::Application.find(params[:id]) |
| 114 | + rescue ActiveRecord::RecordNotFound |
| 115 | + render_404 |
| 116 | + end |
| 117 | + end |
| 118 | +end |
0 commit comments