2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2012 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;
63 if($this->return_module == 'Import'){
64 $url .= "&application={$this->bean->application}&return_module={$this->return_module}&return_action={$this->return_action}";
66 return $this->set_redirect($url);
69 public function pre_save()
72 $this->api = ExternalAPIFactory::loadAPI($this->bean->application,true);
73 if(empty($this->api)) {
74 return $this->failed(translate('LBL_AUTH_UNSUPPORTED', $this->bean->module_dir));
76 if(empty($this->bean->id)){
77 $eapmBean = EAPM::getLoginInfo($this->bean->application,true);
79 SugarApplication::appendErrorMessage(translate('LBL_APPLICATION_FOUND_NOTICE', $this->bean->module_dir));
80 $this->bean->id = $eapmBean->id;
83 $this->bean->validated = false;
84 $this->bean->save_cleanup();
85 $this->api->loadEAPM($this->bean);
88 protected function post_save()
90 if(!$this->bean->deleted) {
91 // do not load bean here since password is already encoded
92 if ( $this->api->authMethod != 'oauth' ) {
93 // OAuth beans have to be handled specially.
95 $reply = $this->api->checkLogin();
96 if ( !$reply['success'] ) {
97 return $this->failed(translate('LBL_AUTH_ERROR', $this->bean->module_dir));
99 $this->bean->validated();
103 if($this->return_module == 'Users'){
104 $this->return_action = 'EditView';
108 if($this->return_module == 'Import'){
109 $this->set_redirect("index.php?module=Import&action=Step1&import_module=". $this->return_action . "&application=" . $this->bean->application);
111 // Override the redirect location to add the hash
112 $this->redirect_url = $this->redirect_url.'#tab5';
113 if ( $this->api->authMethod == 'oauth' && !$this->bean->deleted ) {
114 // It's OAuth, we have to handle this specially.
115 // We need to create a new window to handle the OAuth, and redirect this window back to the edit view
116 // So we will handle that in javascript.
117 $popup_warning_msg = string_format($GLOBALS['mod_strings']['LBL_ERR_POPUPS_DISABLED'], array($_SERVER['HTTP_HOST']) );
118 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>');
120 // To prevent the normal handler from issuing a header call and destroying our neat little javascript we'll
128 protected function action_oauth()
130 if(empty($this->bean->id)) {
131 return $this->set_redirect('index.php');
133 if(!$this->bean->ACLAccess('save')){
134 ACLController::displayNoAccess(true);
138 if(empty($_REQUEST['oauth_error'])) {
139 $this->api = ExternalAPIFactory::loadAPI($this->bean->application,true);
140 $reply = $this->api->checkLogin($this->bean);
141 if ( !$reply['success'] ) {
142 return $this->failed(translate('LBL_AUTH_ERROR', $this->bean->module_dir));
144 $this->bean->validated();
148 // This is a tweak so that we can automatically close windows if requested by the external account system
149 if ( isset($_REQUEST['closeWhenDone']) && $_REQUEST['closeWhenDone'] == 1 ) {
150 if(!empty($_REQUEST['callbackFunction']) && !empty($_REQUEST['application'])){
151 $js = '<script type="text/javascript">window.opener.' . $_REQUEST['callbackFunction'] . '("' . $_REQUEST['application'] . '"); window.close();</script>';
152 }else if(!empty($_REQUEST['refreshParentWindow'])){
153 $js = '<script type="text/javascript">window.opener.location.reload();window.close();</script>';
155 $js = '<script type="text/javascript">window.close();</script>';
161 // redirect to detail view, as in save
162 return parent::post_save();
165 protected function pre_QuickSave(){
166 if(!empty($_REQUEST['application'])){
167 $eapmBean = EAPM::getLoginInfo($_REQUEST['application'],true);
169 $this->bean->application = $_REQUEST['application'];
170 $this->bean->assigned_user_id = $GLOBALS['current_user']->id;
172 $this->bean = $eapmBean;
177 sugar_die("Please pass an application name.");
181 public function action_QuickSave(){
182 $this->api = ExternalAPIFactory::loadAPI($this->bean->application,true);
183 $this->action_save();
185 if ( $this->api->authMethod == 'oauth' ) {
186 $this->action_oauth();
190 protected function post_QuickSave(){
194 protected function pre_Reauthenticate(){
198 protected function action_Reauthenticate(){
199 if ( $this->api->authMethod == 'oauth' ) {
200 // OAuth beans have to be handled specially.
202 $reply = $this->api->checkLogin();
203 if ( !$reply['success'] ) {
204 return $this->failed(translate('LBL_AUTH_ERROR', $this->bean->module_dir));
206 $this->bean->validated();
209 // Normal auth methods go through this.
210 $this->action_save();
214 protected function post_Reauthenticate(){
218 protected function action_FlushFileCache()
220 $api = ExternalAPIFactory::loadAPI($_REQUEST['api']);
221 if ( $api == false ) {
226 if ( method_exists($api,'loadDocCache') ) {
227 $api->loadDocCache(true);
233 protected function remapAction() {
234 if ( $this->do_action == 'DetailView' ) {
235 $this->do_action = 'EditView';
236 $this->action = 'EditView';
239 parent::remapAction();