class.tx_nG6_upgrade.php 5.49 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
/***************************************************************
*  Copyright notice
*
*  (c) 2009 PF bioinformatique de Toulouse <>
*  All rights reserved
*
*  This script is part of the TYPO3 project. The TYPO3 project is
*  free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  The GNU General Public License can be found at
*  http://www.gnu.org/copyleft/gpl.html.
*
*  This script is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/

require_once(PATH_tslib.'class.tslib_pibase.php');

/**
 * Class_nG6_db' for the 'nG6' extension.
 * Allows to migrate usergroups system to (users) rights system.
 *
 * @author	PF bioinformatique de Toulouse <>
 */
class tx_nG6_upgrade {
	
	/**
	 * Main function for upgrade.
	 * 
	 * @param string $version the version 
	 */
	function upgrade($version){
		// version 1.2 : users rights' system 
		if($version == '1.2'){
			tx_nG6_upgrade::upgrade_1_2();
			return "Upgrade successfuly ended to version 1.2!";
		} else {
			return "Upgrading to version " . $version . " not suported!";
		}
	}
	
	
	/*
	* version 1.2
	*------------------------------------------------------------*/
	
	/**
	 * Main function to upgrade to version 1.2.
	 */
	function upgrade_1_2(){

		$queryParts = array(
			'SELECT' => 'uid, fe_group',
			'FROM' => 'tx_nG6_project',
			'WHERE' => '',
			'GROUPBY' => '',
			'ORDERBY' => '',
			'LIMIT' => ''
		);
		$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
		while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
			$project_id = $row['uid'];
			$current_group = $row['fe_group'];
			// users in the current group
			$group_users = tx_nG6_upgrade::get_all_users_in_group($current_group);

			// set current group users' right to "visitor"
			foreach($group_users as $id => $user_id){
				tx_nG6_upgrade::assign_right($user_id, $project_id, 0);
			}
			//assign rights to the super groups users for the specified project id
			tx_nG6_upgrade::assign_super_groups_rights($current_group, $project_id);
		}
		
	}
	
	
	
	/**
	 * Assign the 'administrator' right to all users found in the supergroups of 'basegroup'.
	 * 
	 * @param int $base_group	the base group id
	 * @param int $project_id	the project id
	 */
	function assign_super_groups_rights($base_group, $project_id){		
		$super_groups_ids = tx_nG6_upgrade::get_all_super_groups_of($base_group);
		foreach($super_groups_ids as $id => $group_id){
			$group_users = tx_nG6_upgrade::get_all_users_in_group($group_id);
			// super users = admin
			foreach($group_users as $id => $user_id){
				tx_nG6_upgrade::assign_right($user_id, $project_id, 2);
			}
		}
	}
	
	
	/**
	 * Assign a user right to a specific project.
	 * 
	 * @param int $user_id		the user id
	 * @param int $project_id	the project id
	 * @param int $right		the right id (0=visitor, 1=manager, 2=admininistrator)
	 */
	function assign_right($user_id, $project_id, $right){
		$assign_right = Array(
    		'fe_user_id' 	=> $user_id,
    		'project_id' 	=> $project_id,
    		'right_id'		=> $right
		);
		$GLOBALS['TYPO3_DB']->exec_INSERTquery('fe_rights', $assign_right);
	}	
	
	
	/**
	 * Return all users id from a usergroup. 
	 * 
	 * @param int $group_id the usergroup
	 */
	function get_all_users_in_group($group_id){
		$users = array();
		$queryParts = array(
			'SELECT' => 'uid',
			'FROM' => 'fe_users',
			'WHERE' => 'usergroup='.$group_id,
			'GROUPBY' => '',
			'ORDERBY' => '',
			'LIMIT' => ''
		);
		$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
		while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {			
			$users[] = $row['uid'];
		}
		return $users;
	}
	
	
	
    /**
    * Return the supergroups list of the specified group.
    * 
    * @param string	$group	the group
    * @return array
    */ 
	function get_super_groups_of ($group) {
		// First get the super group of the given group
		$queryParts = array(
			'SELECT' => 'uid, subgroup',
			'FROM' => 'fe_groups',
			'WHERE' => '',
			'GROUPBY' => '',
			'ORDERBY' => '',
			'LIMIT' => ''
		);
		$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
		$reslist = array();
		$i = 0;
		
		while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {			
			if (in_array($group, t3lib_div::intExplode(',',$row['subgroup']))) {
				$reslist[$i] = $row['uid'];
				$i++;
			}
		}
 		return $reslist;
	}
    
	/**
     * Return the recursive supergroups list of the specified group
     *
     * @param string $group the group id
     * @return array
     */
    function get_all_super_groups_of ($group) {
    	// return the recursive list of all $group super groups
    	$first_list = tx_nG6_upgrade::get_super_groups_of($group);
    	$super_list = $first_list;    	
    	foreach ($first_list as $first_list_group_id) {
    		$next_list = tx_nG6_upgrade::get_all_super_groups_of($first_list_group_id, $level);
    		$super_list = array_merge($super_list,$next_list);
    	}
    	return $super_list;
    }
	
}

if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/nG6/class.tx_nG6_upgrade.php']) {
    include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/nG6/class.tx_nG6_upgrade.php']);
}

?>