FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
dbmigrate_2.1-2.2.php
Go to the documentation of this file.
1 <?php
2 /***********************************************************
3  Copyright (C) 2013-2014 Hewlett-Packard Development Company, L.P.
4 
5  This program is free software; you can redistribute it and/or
6  modify it under the terms of the GNU General Public License
7  version 2 as published by the Free Software Foundation.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License along
15  with this program; if not, write to the Free Software Foundation, Inc.,
16  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  ***********************************************************/
19 
41 function Migrate_21_22($Verbose)
42 {
43  global $PG_CONN;
44 
45  // Before adding all the user groups, make sure there is a "Default User" user
46  $user_name = "Default User";
47  $sql = "select user_pk from users where user_name='$user_name'";
48  $result = pg_query($PG_CONN, $sql);
49  DBCheckResult($result, $sql, __FILE__, __LINE__);
50  if (pg_num_rows($result) == 0)
51  {
52  // No Default User, so create one
53  $Perm = 0; //PLUGIN_DB_NONE;
54  $sql = "INSERT INTO users (user_name,user_desc,user_seed,user_pass,user_perm,user_email,root_folder_fk)
55  VALUES ('$user_name','Default User when nobody is logged in','Seed','Pass',$Perm,NULL,1);";
56  $result = pg_query($PG_CONN, $sql);
57  DBCheckResult($result, $sql, __FILE__, __LINE__);
58  }
59  pg_free_result($result);
60 
61  // Before adding all the user groups, make sure there is a "fossy" user
62  $user_name = "fossy";
63  $sql = "select user_pk from users where user_name='$user_name'";
64  $result = pg_query($PG_CONN, $sql);
65  DBCheckResult($result, $sql, __FILE__, __LINE__);
66  if (pg_num_rows($result) == 0)
67  {
68  // No Default User, so create one
69  $Perm = 10; //PLUGIN_DB_ADMIN;
70  $Seed = rand() . rand();
71  $Hash = sha1($Seed . $user_name);
72  $sql = "INSERT INTO users (user_name,user_desc,user_seed,user_pass,user_perm,user_email,root_folder_fk)
73  VALUES ('$user_name','Default Administrator','$Seed','$Hash',$Perm,'y',1);";
74  $result = pg_query($PG_CONN, $sql);
75  DBCheckResult($result, $sql, __FILE__, __LINE__);
76  }
77  pg_free_result($result);
78 
79 
80  $sql = "select user_pk, user_name, root_folder_fk from users";
81  $UserResult = pg_query($PG_CONN, $sql);
82  DBCheckResult($UserResult, $sql, __FILE__, __LINE__);
83 
84  /* Loop through all user records making sure there is a group with the same name,
85  * the group has the user with PERM_WRITE, the user is PERM_ADMIN of their own uploads
86  * and the user has PERM_ADMIN to their root folder (PERM_WRITE if their root folder is
87  * Software Repository.
88  */
89  while($UserRow = pg_fetch_assoc($UserResult))
90  {
91  $user_name = $UserRow['user_name'];
92  $user_pk = $UserRow['user_pk'];
93  $root_folder_fk = $UserRow['root_folder_fk'];
94  $sql = "select group_pk from groups where group_name='$user_name'";
95  $GroupResult = pg_query($PG_CONN, $sql);
96  DBCheckResult($GroupResult, $sql, __FILE__, __LINE__);
97  if (pg_num_rows($GroupResult) == 0)
98  {
99  pg_free_result($GroupResult);
100  /* No group with same name as user, so create one */
101  $sql = "insert into groups(group_name) values ('$user_name')";
102  $result = pg_query($PG_CONN, $sql);
103  DBCheckResult($result, $sql, __FILE__, __LINE__);
104  /* Get new group_pk */
105  $sql = "select group_pk from groups where group_name='$user_name'";
106  $GroupResult = pg_query($PG_CONN, $sql);
107  DBCheckResult($GroupResult, $sql, __FILE__, __LINE__);
108  }
109  $GroupRow = pg_fetch_assoc($GroupResult);
110  $group_pk = $GroupRow['group_pk'];
111  pg_free_result($GroupResult);
112 
113  /* Make sure the user is a member of this group */
114  $sql = "select * from group_user_member where group_fk='$group_pk' and user_fk='$user_pk'";
115  $GroupUserResult = pg_query($PG_CONN, $sql);
116  DBCheckResult($GroupUserResult, $sql, __FILE__, __LINE__);
117  if (pg_num_rows($GroupUserResult) == 0)
118  {
119  /* user is not a member of their own group, so insert them and make them an admin */
120  $sql = "insert into group_user_member(group_fk, user_fk, group_perm) values($group_pk, $user_pk, 1)";
121  $result = pg_query($PG_CONN, $sql);
122  DBCheckResult($result, $sql, __FILE__, __LINE__);
123  }
124  pg_free_result($GroupUserResult);
125 
126  /* Loop through all the uploads for this user and make sure they have permission.
127  * If not, give them PERM_ADMIN
128  */
129  $sql = "select upload_pk from upload where user_fk='$user_pk'";
130  $UploadResult = pg_query($PG_CONN, $sql);
131  DBCheckResult($UploadResult, $sql, __FILE__, __LINE__);
132  while($UploadRow = pg_fetch_assoc($UploadResult))
133  {
134  $upload_pk = $UploadRow['upload_pk'];
135  $sql = "select perm_upload_pk from perm_upload where group_fk='$group_pk' and upload_fk='$upload_pk'";
136  $PermUploadResult = pg_query($PG_CONN, $sql);
137  DBCheckResult($PermUploadResult, $sql, __FILE__, __LINE__);
138  if (pg_num_rows($PermUploadResult) == 0)
139  {
140  $perm_admin = Auth::PERM_ADMIN;
141  $sql = "insert into perm_upload(perm, upload_fk, group_fk) values($perm_admin, $upload_pk, $group_pk)";
142  $result = pg_query($PG_CONN, $sql);
143  DBCheckResult($result, $sql, __FILE__, __LINE__);
144  }
145  pg_free_result($PermUploadResult);
146  }
147  pg_free_result($UploadResult);
148 
149  }
150  pg_free_result($UserResult);
151 
152 
154  $sql = "delete from sysconfig where variablename = 'GlobalBrowse'";
155  $result = pg_query($PG_CONN, $sql);
156  DBCheckResult($result, $sql, __FILE__, __LINE__);
157  pg_free_result($result);
158 
160  $sql = "SELECT conname from pg_constraint where conname= 'license_ref_rf_shortname_key';";
161  $conresult = pg_query($PG_CONN, $sql);
162  DBCheckResult($conresult, $sql, __FILE__, __LINE__);
163  $tt = pg_num_rows($conresult);
164  if (pg_num_rows($conresult) == 0) {
165  $sql = "ALTER TABLE license_ref ADD CONSTRAINT license_ref_rf_shortname_key UNIQUE (rf_shortname); ";
166  $result = pg_query($PG_CONN, $sql);
167  DBCheckResult($result, $sql, __FILE__, __LINE__);
168  pg_free_result($result);
169  }
170  pg_free_result($conresult);
171 
173  $sql = "CREATE VIEW uploadtree_a_upload AS SELECT uploadtree_pk,upload_fk,upload_pk FROM uploadtree_a LEFT OUTER JOIN upload ON upload_fk=upload_pk;
174  DELETE FROM uploadtree_a WHERE uploadtree_pk IN (SELECT uploadtree_pk FROM uploadtree_a_upload WHERE upload_pk IS NULL);
175  DROP VIEW uploadtree_a_upload;";
176  $result = pg_query($PG_CONN, $sql);
177  DBCheckResult($result, $sql, __FILE__, __LINE__);
178  pg_free_result($result);
180  $sql = "SELECT conname from pg_constraint where conname= 'uploadtree_a_upload_fk_fkey';";
181  $conresult = pg_query($PG_CONN, $sql);
182  DBCheckResult($conresult, $sql, __FILE__, __LINE__);
183  if (pg_num_rows($conresult) == 0) {
184  $sql = "ALTER TABLE uploadtree_a ADD CONSTRAINT uploadtree_a_upload_fk_fkey FOREIGN KEY (upload_fk) REFERENCES upload (upload_pk) ON DELETE CASCADE; ";
185  $result = pg_query($PG_CONN, $sql);
186  DBCheckResult($result, $sql, __FILE__, __LINE__);
187  pg_free_result($result);
188  }
189  pg_free_result($conresult);
190 
192  global $MODDIR;
193  global $LIBEXECDIR;
194  $command = "$MODDIR/nomos/agent/nomos -V";
195  $version = exec($command, $out, $return_var);
196  if (0 == $return_var)
197  {
198  $pattern = '/r\((\w+)\)/';
199  preg_match($pattern, $version, $matches);
200 
201  $version230 = 6922;
202 
204  if (empty($matches[1]) || (5 > strlen($matches[1]) && $matches[1] <= $version230)) {
205  require_once("$LIBEXECDIR/fo_mapping_license.php");
206  print "Rename license in $LIBEXECDIR\n";
207  renameLicenses21to22($Verbose);
208  }
209  }
210 
212  if($Verbose)
213  print "Clear out the report cache.\n";
214  $sql = "DELETE FROM report_cache;";
215  $result = pg_query($PG_CONN, $sql);
216  DBCheckResult($result, $sql, __FILE__, __LINE__);
217  pg_free_result($result);
218 
219  return 0; // success
220 } // Migrate_21_22
renameLicenses21to22($verbose)
Create map of old_shortname to new_shortname for 2.1 to 2.2 and call renameLicenses.
Migrate_21_22($Verbose)
Create new groups, group_user_member, perm_upload and perm_folder records to support 2...
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN
DBCheckResult($result, $sql, $filenm, $lineno)
Check the postgres result for unexpected errors. If found, treat them as fatal.
Definition: common-db.php:198
#define PERM_ADMIN
Administrator.
Definition: libfossology.h:46