don't sync groups if no groupDn was given during setup
[tine20] / tine20 / Tinebase / Setup / Initialize.php
1 <?php
2 /**
3  * Tine 2.0
4  * 
5  * @package     Tinebase
6  * @subpackage  Setup
7  * @license     http://www.gnu.org/licenses/agpl.html AGPL Version 3
8  * @author      Jonas Fischer <j.fischer@metaways.de>
9  * @copyright   Copyright (c) 2008-2012 Metaways Infosystems GmbH (http://www.metaways.de)
10  *
11  */
12
13 /**
14  * class for Tinebase initialization
15  * 
16  * @package     Tinebase
17  * @subpackage  Setup
18  */
19 class Tinebase_Setup_Initialize extends Setup_Initialize
20 {
21     /**
22      * Override method: Tinebase needs additional initialisation
23      * 
24      * @see tine20/Setup/Setup_Initialize#_initialize($_application)
25      */
26     public function _initialize(Tinebase_Model_Application $_application, $_options = null)
27     {
28         $this->_initProcedures();
29
30         $this->_setupConfigOptions($_options);
31         $this->_setupGroups();
32         
33         Tinebase_Acl_Roles::getInstance()->createInitialRoles();
34         
35         parent::_initialize($_application, $_options);
36     }
37     
38     /**
39      * This method is necessary only if Oracle is used.
40      * @todo discover a way to replace this code for an equivalent in Tinebase_Backend_Sql_Command
41      */
42     protected function _initProcedures()
43     {
44         $backend = Setup_Backend_Factory::factory();
45         $_db = Tinebase_Core::getDb();
46         if ($_db instanceof Zend_Db_Adapter_Oracle) {
47             $md5 = "CREATE OR REPLACE
48                 function md5( input varchar2 ) return sys.dbms_obfuscation_toolkit.varchar2_checksum as
49                 begin
50                     return lower(rawtohex(utl_raw.cast_to_raw(sys.dbms_obfuscation_toolkit.md5( input_string => input ))));
51                 end;";
52             $backend->execQueryVoid($md5);
53
54             $now = "CREATE OR REPLACE
55                 function NOW return DATE as
56                 begin
57                     return SYSDATE;
58                 end;";
59             $backend->execQueryVoid($now);
60
61             $typeStringAgg = "CREATE OR REPLACE TYPE t_string_agg AS OBJECT
62                     (
63                       g_string  VARCHAR2(32767),
64
65                       STATIC FUNCTION ODCIAggregateInitialize(sctx  IN OUT  t_string_agg)
66                         RETURN NUMBER,
67
68                       MEMBER FUNCTION ODCIAggregateIterate(self   IN OUT  t_string_agg, value  IN      VARCHAR2 )
69                          RETURN NUMBER,
70
71                       MEMBER FUNCTION ODCIAggregateTerminate(self         IN   t_string_agg,
72                                                              returnValue  OUT  VARCHAR2,
73                                                              flags        IN   NUMBER)
74                         RETURN NUMBER,
75
76                       MEMBER FUNCTION ODCIAggregateMerge(self  IN OUT  t_string_agg,
77                                                          ctx2  IN      t_string_agg)
78                         RETURN NUMBER
79                     );";
80             $backend->execQueryVoid($typeStringAgg);
81
82             $typeStringAgg = "CREATE OR REPLACE TYPE BODY t_string_agg IS
83                   STATIC FUNCTION ODCIAggregateInitialize(sctx  IN OUT  t_string_agg)
84                     RETURN NUMBER IS
85                   BEGIN
86                     sctx := t_string_agg(NULL);
87                     RETURN ODCIConst.Success;
88                   END;
89
90                   MEMBER FUNCTION ODCIAggregateIterate(self   IN OUT  t_string_agg,
91                                                        value  IN      VARCHAR2 )
92                     RETURN NUMBER IS
93                   BEGIN
94                     SELF.g_string := self.g_string || ',' || value;
95                     RETURN ODCIConst.Success;
96                   END;
97
98                   MEMBER FUNCTION ODCIAggregateTerminate(self         IN   t_string_agg,
99                                                          returnValue  OUT  VARCHAR2,
100                                                          flags        IN   NUMBER)
101                     RETURN NUMBER IS
102                   BEGIN
103                     returnValue := RTRIM(LTRIM(SELF.g_string, ','), ',');
104                     RETURN ODCIConst.Success;
105                   END;
106
107                   MEMBER FUNCTION ODCIAggregateMerge(self  IN OUT  t_string_agg,
108                                                      ctx2  IN      t_string_agg)
109                     RETURN NUMBER IS
110                   BEGIN
111                     SELF.g_string := SELF.g_string || ',' || ctx2.g_string;
112                     RETURN ODCIConst.Success;
113                   END;
114                 END;";
115             $backend->execQueryVoid($typeStringAgg);
116
117             $group_concat = "CREATE OR REPLACE
118                 FUNCTION GROUP_CONCAT (p_input VARCHAR2)
119                 RETURN VARCHAR2
120                 PARALLEL_ENABLE AGGREGATE USING t_string_agg;";
121             $backend->execQueryVoid($group_concat);
122         }
123     }
124
125     /**
126      * set config options (accounts/authentication/email/...)
127      * 
128      * @param array $_options
129      */
130     protected function _setupConfigOptions($_options)
131     {
132         // ignore empty options
133         foreach($_options as $key => $value) {
134             if (empty($_options[$key])) unset($_options[$key]);
135         }
136         
137         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Saving config options (accounts/authentication/email/...)');
138         
139         // this is a dangerous TRACE as there might be passwords in here!
140         //if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' ' . print_r($_options, TRUE));
141         
142         $defaults = empty($_options['authenticationData']) ? Setup_Controller::getInstance()->loadAuthenticationData() : $_options['authenticationData'];
143         $defaultGroupNames = $this->_parseDefaultGroupNameOptions($_options);
144         $defaults['accounts'][Tinebase_User::getConfiguredBackend()] = array_merge($defaults['accounts'][Tinebase_User::getConfiguredBackend()], $defaultGroupNames);
145         
146         $emailConfigKeys = Setup_Controller::getInstance()->getEmailConfigKeys();
147         $configsToSet = array_merge($emailConfigKeys, array('authentication', 'accounts', 'redirectSettings', 'acceptedTermsVersion'));
148         
149         if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' ' . print_r($configsToSet, TRUE));
150         if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' ' . print_r($defaults, TRUE));
151         
152         $optionsToSave = array();
153         foreach ($configsToSet as $group) {
154             if (isset($_options[$group])) {
155                 $parsedOptions = (is_string($_options[$group])) ? Setup_Frontend_Cli::parseConfigValue($_options[$group]) : $_options[$group];
156                 
157                 switch ($group) {
158                     case 'authentication':
159                     case 'accounts':
160                         $backend = (isset($parsedOptions['backend'])) ? ucfirst($parsedOptions['backend']) : Tinebase_User::SQL;
161                         $optionsToSave[$group][$backend] = (isset($parsedOptions[$backend])) ? $parsedOptions[$backend] : $parsedOptions;
162                         $optionsToSave[$group]['backend'] = $backend;
163                         break;
164                     default:
165                         $optionsToSave[$group] = $parsedOptions;
166                 }
167             } else if (isset($defaults[$group])) {
168                 $optionsToSave[$group] = $defaults[$group];
169             }
170         }
171         
172         if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' ' . print_r($optionsToSave, TRUE));
173         
174         Setup_Controller::getInstance()->saveEmailConfig($optionsToSave);
175         Setup_Controller::getInstance()->saveAuthentication($optionsToSave);
176     }
177     
178     /**
179     * Extract default group name settings from {@param $_options}
180     *
181     * @param array $_options
182     * @return array
183     */
184     protected function _parseDefaultGroupNameOptions($_options)
185     {
186         $result = array(
187             'defaultAdminGroupName' => (isset($_options['defaultAdminGroupName'])) ? $_options['defaultAdminGroupName'] : Tinebase_Group::DEFAULT_ADMIN_GROUP,
188             'defaultUserGroupName'  => (isset($_options['defaultUserGroupName'])) ? $_options['defaultUserGroupName'] : Tinebase_Group::DEFAULT_USER_GROUP,
189         );
190         
191         return $result;
192     }
193     
194     /**
195      * import groups(ldap)/create initial groups(sql)
196      * 
197      * @todo allow to configure if groups should be synced?
198      */
199     protected function _setupGroups()
200     {
201         if (Tinebase_Group::getInstance() instanceof Tinebase_Group_Interface_SyncAble && ! Tinebase_Group::getInstance()->isDisabledBackend()) {
202             Tinebase_Group::syncGroups();
203         } else {
204             Tinebase_Group::createInitialGroups();
205         }
206     }
207     
208     /**
209      * Override method because this app requires special rights
210      * @see tine20/Setup/Setup_Initialize#_createInitialRights($_application)
211      * 
212      * @todo make hard coded role name ('user role') configurable
213      */
214     protected function _createInitialRights(Tinebase_Model_Application $_application)
215     {
216         parent::_createInitialRights($_application);
217
218         $roles = Tinebase_Acl_Roles::getInstance();
219         $userRole = $roles->getRoleByName('user role');
220         $roles->addSingleRight(
221             $userRole->getId(), 
222             $_application->getId(), 
223             Tinebase_Acl_Rights::CHECK_VERSION
224         );
225         $roles->addSingleRight(
226             $userRole->getId(), 
227             $_application->getId(), 
228             Tinebase_Acl_Rights::REPORT_BUGS
229         );
230         $roles->addSingleRight(
231             $userRole->getId(), 
232             $_application->getId(), 
233             Tinebase_Acl_Rights::MANAGE_OWN_STATE
234         );
235     }
236     
237     /**
238      * init scheduler tasks
239      */
240     protected function _initializeSchedulerTasks()
241     {
242         $scheduler = Tinebase_Core::getScheduler();
243         
244         Tinebase_Scheduler_Task::addAlarmTask($scheduler);
245         Tinebase_Scheduler_Task::addCacheCleanupTask($scheduler);
246         Tinebase_Scheduler_Task::addCredentialCacheCleanupTask($scheduler);
247         Tinebase_Scheduler_Task::addTempFileCleanupTask($scheduler);
248         Tinebase_Scheduler_Task::addDeletedFileCleanupTask($scheduler);
249         Tinebase_Scheduler_Task::addSessionsCleanupTask($scheduler);
250         Tinebase_Scheduler_Task::addAccessLogCleanupTask($scheduler);
251     }
252 }