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 if($this->module == 'EAPM') {
112 $this->set_redirect('index.php?module=Users&action=EditView&record=' . $_POST['assigned_user_id']);
114 // Override the redirect location to add the hash
115 $this->redirect_url = $this->redirect_url.'#tab5';
116 if ( $this->api->authMethod == 'oauth' && !$this->bean->deleted ) {
117 // It's OAuth, we have to handle this specially.
118 // We need to create a new window to handle the OAuth, and redirect this window back to the edit view
119 // So we will handle that in javascript.
120 $popup_warning_msg = string_format($GLOBALS['mod_strings']['LBL_ERR_POPUPS_DISABLED'], array($_SERVER['HTTP_HOST']) );
121 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>');
123 // To prevent the normal handler from issuing a header call and destroying our neat little javascript we'll
131 protected function action_oauth()
133 if(empty($this->bean->id)) {
134 return $this->set_redirect('index.php');
136 if(!$this->bean->ACLAccess('save')){
137 ACLController::displayNoAccess(true);
141 if(empty($_REQUEST['oauth_error'])) {
142 $this->api = ExternalAPIFactory::loadAPI($this->bean->application,true);
143 $reply = $this->api->checkLogin($this->bean);
144 if ( !$reply['success'] ) {
145 return $this->failed(translate('LBL_AUTH_ERROR', $this->bean->module_dir));
147 $this->bean->validated();
151 // This is a tweak so that we can automatically close windows if requested by the external account system
152 if ( isset($_REQUEST['closeWhenDone']) && $_REQUEST['closeWhenDone'] == 1 ) {
153 if(!empty($_REQUEST['callbackFunction']) && !empty($_REQUEST['application'])){
154 $js = '<script type="text/javascript">window.opener.' . $_REQUEST['callbackFunction'] . '("' . $_REQUEST['application'] . '"); window.close();</script>';
155 }else if(!empty($_REQUEST['refreshParentWindow'])){
156 $js = '<script type="text/javascript">window.opener.location.reload();window.close();</script>';
158 $js = '<script type="text/javascript">window.close();</script>';
164 // redirect to detail view, as in save
165 return parent::post_save();
168 protected function pre_QuickSave(){
169 if(!empty($_REQUEST['application'])){
170 $eapmBean = EAPM::getLoginInfo($_REQUEST['application'],true);
172 $this->bean->application = $_REQUEST['application'];
173 $this->bean->assigned_user_id = $GLOBALS['current_user']->id;
175 $this->bean = $eapmBean;
180 sugar_die("Please pass an application name.");
184 public function action_QuickSave(){
185 $this->api = ExternalAPIFactory::loadAPI($this->bean->application,true);
186 $this->action_save();
188 if ( $this->api->authMethod == 'oauth' ) {
189 $this->action_oauth();
193 protected function post_QuickSave(){
197 protected function pre_Reauthenticate(){
201 protected function action_Reauthenticate(){
202 if ( $this->api->authMethod == 'oauth' ) {
203 // OAuth beans have to be handled specially.
205 $reply = $this->api->checkLogin();
206 if ( !$reply['success'] ) {
207 return $this->failed(translate('LBL_AUTH_ERROR', $this->bean->module_dir));
209 $this->bean->validated();
212 // Normal auth methods go through this.
213 $this->action_save();
217 protected function post_Reauthenticate(){
221 protected function action_FlushFileCache()
223 $api = ExternalAPIFactory::loadAPI($_REQUEST['api']);
224 if ( $api == false ) {
229 if ( method_exists($api,'loadDocCache') ) {
230 $api->loadDocCache(true);
236 protected function remapAction() {
237 if ( $this->do_action == 'DetailView' ) {
238 $this->do_action = 'EditView';
239 $this->action = 'EditView';
242 parent::remapAction();