]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/EAPM/controller.php
Release 6.2.0beta4
[Github/sugarcrm.git] / modules / EAPM / controller.php
1 <?PHP
2 /*********************************************************************************
3  * SugarCRM is a customer relationship management program developed by
4  * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
5  * 
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.
12  * 
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
16  * details.
17  * 
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
21  * 02110-1301 USA.
22  * 
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.
25  * 
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.
29  * 
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  ********************************************************************************/
36
37
38 class EAPMController extends SugarController
39 {
40     /**
41      * API implementation
42      * @var ExternalAPIPlugin
43      */
44     protected $api;
45
46     var $admin_actions = array('listview', 'index');
47
48         public function process() {
49                 if(!is_admin($GLOBALS['current_user']) && in_array(strtolower($this->action), $this->admin_actions)) {
50                         $this->hasAccess = false;
51                 }
52                 parent::process();
53         }
54
55     protected function failed($error)
56     {
57         SugarApplication::appendErrorMessage($error);
58         $GLOBALS['log']->error("Login error: $error");
59         $url = 'index.php?module=EAPM&action=EditView&record='.$this->bean->id;
60         return $this->set_redirect($url);
61     }
62
63     public function pre_save()
64     {
65         parent::pre_save();
66         $this->api = ExternalAPIFactory::loadAPI($this->bean->application,true);
67         if(empty($this->api)) {
68             return $this->failed(translate('LBL_AUTH_UNSUPPORTED', $this->bean->module_dir));
69         }
70         if(empty($this->bean->id)){
71             $eapmBean = EAPM::getLoginInfo($this->bean->application,true);
72             if($eapmBean){
73                 SugarApplication::appendErrorMessage(translate('LBL_APPLICATION_FOUND_NOTICE', $this->bean->module_dir));
74                 $this->bean->id = $eapmBean->id;
75             }
76         }
77         $this->bean->validated = false;
78         $this->bean->save_cleanup();
79         $this->api->loadEAPM($this->bean);
80     }
81
82     protected function post_save()
83     {
84         if($this->bean->active) {
85             // do not load bean here since password is already encoded
86             $reply = $this->api->checkLogin();
87             if ( !$reply['success'] ) {
88                 return $this->failed(translate('LBL_AUTH_ERROR', $this->bean->module_dir));
89             } else {
90                 $this->bean->validated();
91             }
92         }
93         if($this->return_module == 'Users'){
94             $this->return_action = 'EditView';
95         }
96         return parent::post_save();
97     }
98
99     protected function action_oauth()
100     {
101         if(empty($this->bean->id)) {
102             return $this->set_redirect('index.php');
103         }
104                 if(!$this->bean->ACLAccess('save')){
105                         ACLController::displayNoAccess(true);
106                         sugar_cleanup(true);
107                         return true;
108                 }
109         $this->api = ExternalAPIFactory::loadAPI($this->bean->application,true);
110         $reply = $this->api->checkLogin($this->bean);
111         if ( !$reply['success'] ) {
112             return $this->failed(translate('LBL_AUTH_ERROR', $this->bean->module_dir));
113         } else {
114             $this->bean->validated();
115             
116             // This is a tweak so that we can automatically close windows if requested by the external account system
117             if ( isset($_REQUEST['closeWhenDone']) && $_REQUEST['closeWhenDone'] == 1 ) {
118                 if(!empty($_REQUEST['callbackFunction']) && !empty($_REQUEST['application'])){
119                     $js = '<script type="text/javascript">window.opener.' . $_REQUEST['callbackFunction'] . '("' . $_REQUEST['application'] . '"); window.close();</script>';
120                 }else if(!empty($_REQUEST['refreshParentWindow'])){
121                     $js = '<script type="text/javascript">window.opener.location.reload();window.close();</script>';
122                 }else{
123                     $js = '<script type="text/javascript">window.close();</script>';
124                 }
125                 echo($js);
126                 return;
127             }            
128
129             // redirect to detail view, as in save
130             return parent::post_save();
131         }
132     }
133
134     protected function pre_QuickSave(){
135         if(!empty($_REQUEST['application'])){
136             $eapmBean = EAPM::getLoginInfo($_REQUEST['application'],true);
137             if (!$eapmBean) {
138                 $this->bean->application = $_REQUEST['application'];
139                 $this->bean->assigned_user_id = $GLOBALS['current_user']->id;
140             }else{
141                 $this->bean = $eapmBean;
142                 $this->bean->active = 1;
143             }
144             $this->pre_save();
145                     
146         }else{
147             sugar_die("Please pass an application name.");
148         }
149     }
150     
151         public function action_QuickSave(){
152                 $this->action_save();
153         }
154
155     protected function post_QuickSave(){
156         $this->post_save();
157     }
158
159     protected function pre_Reauthenticate(){
160         $this->bean->active = 1;
161         $this->pre_save();
162     }
163
164     protected function action_Reauthenticate(){
165         $this->action_save();
166     }
167
168     protected function post_Reauthenticate(){
169         $this->post_save();
170     }
171
172     protected function action_FlushFileCache()
173     {
174         $api = ExternalAPIFactory::loadAPI($_REQUEST['api']);
175         if ( $api == false ) {
176             echo 'FAILED';
177             return;
178         }
179
180         if ( method_exists($api,'loadDocCache') ) {
181             $api->loadDocCache(true);
182         }
183
184         echo 'SUCCESS';
185     }
186 }