7002947710d9b69267ccdec5d034d113512e7265
[atutor.git] / mods / wiki / plugins / auth-liveuser / liveuser_setup.php
1 <?php
2
3 /**
4  * Copyright (c) 2003, The Burgiss Group, LLC
5  * This source code is part of eWiki LiveUser Plugin.
6  *
7  * eWiki LiveUser Plugin is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or (at your
10  * option) any later version.
11  *
12  * eWiki LiveUser Plugin is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with Wiki LiveUser Plugin; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 /*
23  * note: this setup file will build database tables used by this plugin (LiveUser
24  * tables must be generated separately), and populate the tables with the configuration
25  * specified herein. This script is intended to be run from the command line, and will
26  * generate PHP code which should be inserted into liveuser_conf.php, as it will contain
27  * several defined constants. This file should be edited to customize rights, groups, and
28  * users. Further configuration may be done after the installation process via the
29  * admin gui plugin pages.
30  *
31  * the defined constants must be kept in sync with the database to ensure proper
32  * operation of this plugin. for this reason, it is possible to run this script
33  * over again to regenerate the definition code based on the current database
34  * state; the current id's for each entity (based on a name match) would be retrieved,
35  * and any entities that did not exist would be created again.
36  *
37  * specifically, the only necessary defined constants include the 'AREA' entity, and
38  * the LoggedIn, NotLoggedIn, and 'LW_'-prefixed rights. While other constants
39  * specified in this script are outputted as a convenience, they are not required
40  * to be inserted into liveuser_conf.php.
41  */
42
43 require_once('LiveUser/LiveUser.php');
44 require_once('LiveUser/Admin/Auth/Container/DB.php');
45 require_once('LiveUser/Admin/Perm/Container/DB_Complex.php');
46 require_once(dirname(__FILE__).'/liveuser_aux.php');
47
48 // fetch a list of all existing wiki pagenames, which will be given default permissions
49 $liveuserWikiDB = DB::connect('type://username:password@server/database');
50 $liveuserWikiDB->setFetchMode(DB_FETCHMODE_ASSOC);
51 $pagenames = $liveuserWikiDB->getAll('SELECT DISTINCT pagename FROM ewiki');
52 $liveuserWikiDB->disconnect();
53
54 /*
55  * if necessary, override the database connection in liveuser_conf.php with one
56  * that has CREATE/DROP rights. comment these lines otherwise.
57  */
58 // $liveuserDB->disconnect();
59 // $liveuserDB =& DB::connect('type://username:password@server/database');
60
61 // create plugin tables
62 $liveuserDB->query('
63    CREATE TABLE IF NOT EXISTS '.LW_PREFIX.'_perms (
64    id int(10) unsigned not null auto_increment,
65    pagename varchar(160) binary not null,
66    ring tinyint(3) unsigned not null,
67    right_id int(10) not null,
68    primary key (id),
69    unique pagerule (pagename, right_id))
70    type=MyISAM;');
71
72 // liveuser prefs plugin tables
73 $liveuserDB->query('
74    CREATE TABLE IF NOT EXISTS '.LW_PREFIX.'_prefs_data (
75    pref_id int(10) unsigned not null auto_increment,
76    user_id int(10) not null,
77    field_id int(10) not null,
78    field_value mediumblob,
79    primary key (pref_id),
80    unique prefrule (user_id, field_id))
81    type=MyISAM;');
82    
83 $liveuserDB->query('
84    CREATE TABLE IF NOT EXISTS '.LW_PREFIX.'_prefs_fields (
85    field_id int(10) unsigned not null auto_increment,
86    field_name varchar(255),
87    public tinyint(1),
88    default_value mediumblob,
89    primary key (field_id),
90    unique fieldrule (field_name))
91    type=MyISAM;');  
92
93 $liveuserDB->query("
94    CREATE TABLE IF NOT EXISTS ".LW_PREFIX."_login_log (
95   auth_user_handle varchar(32) NOT NULL default '',
96   php_session_id varchar(40) NOT NULL default '',
97   ssl_session_id varchar(16) default '',
98   ip_address varchar(16) NOT NULL default '',
99   time timestamp(14) NOT NULL,
100   delay tinyint(1) NOT NULL default '0',
101   success tinyint(1) NOT NULL default '0',
102   PRIMARY KEY  (time,auth_user_handle)
103   ) TYPE=MyISAM; ");
104
105
106 /*
107  * the defs array will accumulate all created entities, that should become defined
108  * constants to be inserted into the main config file. created entities will include
109  * the language (default English, also set in liveuser_aux.php), application
110  * name and area (arbitrary values), right and group names, and users. All groups
111  * should correspond to a particular right of the same name.
112  */
113 $defs = array();
114
115 $defs['LU_LANG'] = liveuser_addEntity('language', array('EN', 'English', 'English'));
116 $defs['LU_APP']  = liveuser_addEntity('application', array('LU_APP', 'AppName'));
117 $defs['LU_AREA'] = liveuser_addEntity('area', array($defs['LU_APP'], 'LU_AREA', 'AppArea'));
118
119 $rights = array(
120     array($defs['LU_AREA'], 'LU_R_NOTLOGGEDIN', 'NotLoggedIn'),
121     array($defs['LU_AREA'], 'LU_R_LOGGEDIN', 'LoggedIn'),
122     array($defs['LU_AREA'], 'LU_R_LW_PUBLISHER', 'LW_Publisher'),
123     array($defs['LU_AREA'], 'LU_R_LW_ADMIN', 'LW_Admin'),
124     array($defs['LU_AREA'], 'LU_R_STAFF', 'Staff'));
125     
126 foreach ($rights as $right) {
127     $defs[$right[1]] = liveuser_addEntity('right', $right);
128 }
129
130 $groups = array(
131     array('LU_G_LW_PUBLISHER', 'LW_Publisher', null, true),    
132     array('LU_G_LW_ADMIN', 'LW_Admin', null, true),
133     array('LU_G_STAFF', 'Staff', null, true));
134     
135 foreach ($groups as $group) {
136     $setup_defs[$group[0]] = liveuser_addEntity('group', $group);
137     
138     if (liveuser_checkGroupRight($defs[$group[0]], $defs['LU_R_'.strtoupper($group[1])]) === false) {
139         $liveuserPermAdmin->grantGroupRight($defs[$group[0]], $defs['LU_R_'.strtoupper($group[1])], 1);
140     }    
141
142
143 // LiveUser plugin pages
144 liveuser_addPerm('AdminPerms', $liveuserBaseRings['view'], $defs['LU_R_LW_ADMIN']);
145 liveuser_addPerm('AdminRights', $liveuserBaseRings['view'], $defs['LU_R_LW_ADMIN']);
146 liveuser_addPerm('AdminAddUsers', $liveuserBaseRings['view'], $defs['LU_R_LW_ADMIN']);
147 liveuser_addPerm('AdminUsers', $liveuserBaseRings['view'], $defs['LU_R_LW_ADMIN']);
148 // default list of eWiki plugins
149 liveuser_addPerm('LogIn', $liveuserBaseRings['view'], $defs['LU_R_NOTLOGGEDIN']);
150 liveuser_addPerm('Login', $liveuserBaseRings['view'], $defs['LU_R_NOTLOGGEDIN']);
151 liveuser_addPerm('LogOut', $liveuserBaseRings['view'], $defs['LU_R_LOGGEDIN']);
152 liveuser_addPerm('Logout', $liveuserBaseRings['view'], $defs['LU_R_LOGGEDIN']);
153 liveuser_addPerm('ChangePassword', $liveuserBaseRings['view'], $defs['LU_R_LOGGEDIN']);
154 liveuser_addPerm('ProtectedEmail', $liveuserBaseRings['view'], $defs['LU_R_NOTLOGGEDIN']);
155 liveuser_addPerm('PowerSearch', $liveuserBaseRings['view'], $defs['LU_R_LOGGEDIN']);
156 liveuser_addPerm('SearchPages', $liveuserBaseRings['view'], $defs['LU_R_LOGGEDIN']);
157 liveuser_addPerm('Search', $liveuserBaseRings['view'], $defs['LU_R_LOGGEDIN']);
158 liveuser_addPerm('PageIndex', $liveuserBaseRings['view'], $defs['LU_R_STAFF']);
159 liveuser_addPerm('WordIndex', $liveuserBaseRings['view'], $defs['LU_R_STAFF']);
160 liveuser_addPerm('PageCalendar', $liveuserBaseRings['view'], $defs['LU_R_STAFF']);
161 liveuser_addPerm('PageYearCalendar', $liveuserBaseRings['view'], $defs['LU_R_STAFF']);
162 liveuser_addPerm('OrphanedPages', $liveuserBaseRings['view'], $defs['LU_R_STAFF']);
163 liveuser_addPerm('NewestPages', $liveuserBaseRings['view'], $defs['LU_R_STAFF']);
164 liveuser_addPerm('MostVisitedPages', $liveuserBaseRings['view'], $defs['LU_R_STAFF']);
165 liveuser_addPerm('MostOftenChangedPages', $liveuserBaseRings['view'], $defs['LU_R_STAFF']);
166 liveuser_addPerm('UpdatedPages', $liveuserBaseRings['view'], $defs['LU_R_STAFF']);
167 liveuser_addPerm('FileUpload', $liveuserBaseRings['view'], $defs['LU_R_STAFF']);
168 liveuser_addPerm('FileDownload', $liveuserBaseRings['view'], $defs['LU_R_STAFF']);
169 liveuser_addPerm('AboutPlugins', $liveuserBaseRings['view'], $defs['LU_R_STAFF']);
170 // ability to create new pages
171 liveuser_addPerm('[NewPage]', $liveuserBaseRings['manage'], $defs['LU_R_STAFF']);
172
173 // set rights on all existing pagename's 
174 foreach ($pagenames as $pagename) {    
175     liveuser_addPerm($pagename['pagename'], $liveuserBaseRings['manage'], $defs['LU_R_STAFF']);
176 }
177
178 echo "// add the following lines to liveuser_conf.php\n";
179 foreach ($defs as $key => $value) {
180     echo 'define(\''.$key.'\', '.(is_numeric($value) ? $value : '\''.$value.'\'').");\n";
181     define($key,$value);
182 }
183
184 // default user list
185 $users = array(
186     'user1',
187     'user2',
188     'user3');
189
190 // add users (password = username) and set group memberships for default users
191 foreach ($users as $user) {
192     if (liveuser_checkEntity('user', $user) === false) {
193         if (liveuser_addEntity('user', array($user, $user)) !== false) {
194             echo 'added user '.$user."\n";
195         }
196         
197         $id = liveuser_checkEntity('user', $user);
198         if ($id !== false && $liveuserPermAdmin->addUserToGroup($id, $setup_defs['LU_G_STAFF'])) {
199             echo 'added user '.$user.' to group Staff'."\n";    
200         }       
201     }
202 }
203
204 // set special group membership for a user
205 $user1 = liveuser_checkEntity('user', 'user1');
206 if (liveuser_checkGroupUser($setup_defs['LU_G_LW_PUBLISHER'], $user1) === false) {
207     if ($liveuserPermAdmin->addUserToGroup($user1, $setup_defs['LU_G_LW_PUBLISHER'])) {
208         echo 'added user user1 to group Publisher'."\n";
209     }   
210 }
211 if (liveuser_checkGroupUser($setup_defs['LU_G_LW_ADMIN'], $user1) === false) {
212     if ($liveuserPermAdmin->addUserToGroup($user1, $setup_defs['LU_G_LW_ADMIN'])) {
213         echo 'added user user1 to group Admin'."\n";
214     }   
215 }
216
217 ?>