2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
6 * This program is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU Affero General Public License version 3 as published by the
8 * Free Software Foundation with the addition of the following permission added
9 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
11 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
18 * You should have received a copy of the GNU Affero General Public License along with
19 * this program; if not, see http://www.gnu.org/licenses or write to the Free
20 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
24 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26 * The interactive user interfaces in modified source and object code versions
27 * of this program must display Appropriate Legal Notices, as required under
28 * Section 5 of the GNU Affero General Public License version 3.
30 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31 * these Appropriate Legal Notices must retain the display of the "Powered by
32 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
33 * technical reasons, the Appropriate Legal Notices must display the words
34 * "Powered by SugarCRM".
35 ********************************************************************************/
38 class EAPMController extends SugarController
42 * @var ExternalAPIPlugin
46 var $action_remap = array('detailview'=>'editview', 'DetailView'=>'EditView');
48 var $admin_actions = array('listview', 'index');
50 public function process() {
51 if(!is_admin($GLOBALS['current_user']) && in_array(strtolower($this->action), $this->admin_actions)) {
52 $this->hasAccess = false;
57 protected function failed($error)
59 SugarApplication::appendErrorMessage($error);
60 $GLOBALS['log']->error("Login error: $error");
61 $url = 'index.php?module=EAPM&action=EditView&record='.$this->bean->id;
62 return $this->set_redirect($url);
65 public function pre_save()
68 $this->api = ExternalAPIFactory::loadAPI($this->bean->application,true);
69 if(empty($this->api)) {
70 return $this->failed(translate('LBL_AUTH_UNSUPPORTED', $this->bean->module_dir));
72 if(empty($this->bean->id)){
73 $eapmBean = EAPM::getLoginInfo($this->bean->application,true);
75 SugarApplication::appendErrorMessage(translate('LBL_APPLICATION_FOUND_NOTICE', $this->bean->module_dir));
76 $this->bean->id = $eapmBean->id;
79 $this->bean->validated = false;
80 $this->bean->save_cleanup();
81 $this->api->loadEAPM($this->bean);
84 protected function post_save()
86 if(!$this->bean->deleted) {
87 // do not load bean here since password is already encoded
88 if ( $this->api->authMethod != 'oauth' ) {
89 // OAuth beans have to be handled specially.
91 $reply = $this->api->checkLogin();
92 if ( !$reply['success'] ) {
93 return $this->failed(translate('LBL_AUTH_ERROR', $this->bean->module_dir));
95 $this->bean->validated();
99 if($this->return_module == 'Users'){
100 $this->return_action = 'EditView';
103 // Override the redirect location to add the hash
104 $this->redirect_url = $this->redirect_url.'#tab5';
105 if ( $this->api->authMethod == 'oauth' && !$this->bean->deleted ) {
106 // It's OAuth, we have to handle this specially.
107 // We need to create a new window to handle the OAuth, and redirect this window back to the edit view
108 // So we will handle that in javascript.
109 $popup_warning_msg = string_format($GLOBALS['mod_strings']['LBL_ERR_POPUPS_DISABLED'], array($_SERVER['HTTP_HOST']) );
110 echo('<script src="modules/EAPM/EAPMEdit.js" type="text/javascript"></script><script type="text/javascript">EAPMPopupAndRedirect("index.php?module=EAPM&action=oauth&record='.$this->bean->id.'", "'.$this->redirect_url.'", \''.$popup_warning_msg.'\'); </script>');
112 // To prevent the normal handler from issuing a header call and destroying our neat little javascript we'll
120 protected function action_oauth()
122 if(empty($this->bean->id)) {
123 return $this->set_redirect('index.php');
125 if(!$this->bean->ACLAccess('save')){
126 ACLController::displayNoAccess(true);
130 if(empty($_REQUEST['oauth_error'])) {
131 $this->api = ExternalAPIFactory::loadAPI($this->bean->application,true);
132 $reply = $this->api->checkLogin($this->bean);
133 if ( !$reply['success'] ) {
134 return $this->failed(translate('LBL_AUTH_ERROR', $this->bean->module_dir));
136 $this->bean->validated();
140 // This is a tweak so that we can automatically close windows if requested by the external account system
141 if ( isset($_REQUEST['closeWhenDone']) && $_REQUEST['closeWhenDone'] == 1 ) {
142 if(!empty($_REQUEST['callbackFunction']) && !empty($_REQUEST['application'])){
143 $js = '<script type="text/javascript">window.opener.' . $_REQUEST['callbackFunction'] . '("' . $_REQUEST['application'] . '"); window.close();</script>';
144 }else if(!empty($_REQUEST['refreshParentWindow'])){
145 $js = '<script type="text/javascript">window.opener.location.reload();window.close();</script>';
147 $js = '<script type="text/javascript">window.close();</script>';
153 // redirect to detail view, as in save
154 return parent::post_save();
157 protected function pre_QuickSave(){
158 if(!empty($_REQUEST['application'])){
159 $eapmBean = EAPM::getLoginInfo($_REQUEST['application'],true);
161 $this->bean->application = $_REQUEST['application'];
162 $this->bean->assigned_user_id = $GLOBALS['current_user']->id;
164 $this->bean = $eapmBean;
169 sugar_die("Please pass an application name.");
173 public function action_QuickSave(){
174 $this->api = ExternalAPIFactory::loadAPI($this->bean->application,true);
175 $this->action_save();
177 if ( $this->api->authMethod == 'oauth' ) {
178 $this->action_oauth();
182 protected function post_QuickSave(){
186 protected function pre_Reauthenticate(){
190 protected function action_Reauthenticate(){
191 if ( $this->api->authMethod == 'oauth' ) {
192 // OAuth beans have to be handled specially.
194 $reply = $this->api->checkLogin();
195 if ( !$reply['success'] ) {
196 return $this->failed(translate('LBL_AUTH_ERROR', $this->bean->module_dir));
198 $this->bean->validated();
201 // Normal auth methods go through this.
202 $this->action_save();
206 protected function post_Reauthenticate(){
210 protected function action_FlushFileCache()
212 $api = ExternalAPIFactory::loadAPI($_REQUEST['api']);
213 if ( $api == false ) {
218 if ( method_exists($api,'loadDocCache') ) {
219 $api->loadDocCache(true);
225 protected function remapAction() {
226 if ( $this->do_action == 'DetailView' ) {
227 $this->do_action = 'EditView';
228 $this->action = 'EditView';
231 parent::remapAction();