FOSSology  4.4.0
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  SPDX-FileCopyrightText: © 2013-2014 Hewlett-Packard Development Company, L.P.
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
8 
30 function Migrate_21_22($Verbose)
31 {
32  global $PG_CONN;
33 
34  // Before adding all the user groups, make sure there is a "Default User" user
35  $user_name = "Default User";
36  $sql = "select user_pk from users where user_name='$user_name'";
37  $result = pg_query($PG_CONN, $sql);
38  DBCheckResult($result, $sql, __FILE__, __LINE__);
39  if (pg_num_rows($result) == 0)
40  {
41  // No Default User, so create one
42  $Perm = 0; //PLUGIN_DB_NONE;
43  $sql = "INSERT INTO users (user_name,user_desc,user_seed,user_pass,user_perm,user_email,root_folder_fk)
44  VALUES ('$user_name','Default User when nobody is logged in','Seed','Pass',$Perm,NULL,1);";
45  $result = pg_query($PG_CONN, $sql);
46  DBCheckResult($result, $sql, __FILE__, __LINE__);
47  }
48  pg_free_result($result);
49 
50  // Before adding all the user groups, make sure there is a "fossy" user
51  $user_name = "fossy";
52  $sql = "select user_pk from users where user_name='$user_name'";
53  $result = pg_query($PG_CONN, $sql);
54  DBCheckResult($result, $sql, __FILE__, __LINE__);
55  if (pg_num_rows($result) == 0)
56  {
57  // No Default User, so create one
58  $Perm = 10; //PLUGIN_DB_ADMIN;
59  $Seed = random_int(0, getrandmax()) . random_int(0, getrandmax());
60  $Hash = sha1($Seed . $user_name);
61  $sql = "INSERT INTO users (user_name,user_desc,user_seed,user_pass,user_perm,user_email,root_folder_fk)
62  VALUES ('$user_name','Default Administrator','$Seed','$Hash',$Perm,'y',1);";
63  $result = pg_query($PG_CONN, $sql);
64  DBCheckResult($result, $sql, __FILE__, __LINE__);
65  }
66  pg_free_result($result);
67 
68 
69  $sql = "select user_pk, user_name, root_folder_fk from users";
70  $UserResult = pg_query($PG_CONN, $sql);
71  DBCheckResult($UserResult, $sql, __FILE__, __LINE__);
72 
73  /* Loop through all user records making sure there is a group with the same name,
74  * the group has the user with PERM_WRITE, the user is PERM_ADMIN of their own uploads
75  * and the user has PERM_ADMIN to their root folder (PERM_WRITE if their root folder is
76  * Software Repository.
77  */
78  while($UserRow = pg_fetch_assoc($UserResult))
79  {
80  $user_name = $UserRow['user_name'];
81  $user_pk = $UserRow['user_pk'];
82  $root_folder_fk = $UserRow['root_folder_fk'];
83  $sql = "select group_pk from groups where group_name='$user_name'";
84  $GroupResult = pg_query($PG_CONN, $sql);
85  DBCheckResult($GroupResult, $sql, __FILE__, __LINE__);
86  if (pg_num_rows($GroupResult) == 0)
87  {
88  pg_free_result($GroupResult);
89  /* No group with same name as user, so create one */
90  $sql = "insert into groups(group_name) values ('$user_name')";
91  $result = pg_query($PG_CONN, $sql);
92  DBCheckResult($result, $sql, __FILE__, __LINE__);
93  /* Get new group_pk */
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  }
98  $GroupRow = pg_fetch_assoc($GroupResult);
99  $group_pk = $GroupRow['group_pk'];
100  pg_free_result($GroupResult);
101 
102  /* Make sure the user is a member of this group */
103  $sql = "select * from group_user_member where group_fk='$group_pk' and user_fk='$user_pk'";
104  $GroupUserResult = pg_query($PG_CONN, $sql);
105  DBCheckResult($GroupUserResult, $sql, __FILE__, __LINE__);
106  if (pg_num_rows($GroupUserResult) == 0)
107  {
108  /* user is not a member of their own group, so insert them and make them an admin */
109  $sql = "insert into group_user_member(group_fk, user_fk, group_perm) values($group_pk, $user_pk, 1)";
110  $result = pg_query($PG_CONN, $sql);
111  DBCheckResult($result, $sql, __FILE__, __LINE__);
112  }
113  pg_free_result($GroupUserResult);
114 
115  /* Loop through all the uploads for this user and make sure they have permission.
116  * If not, give them PERM_ADMIN
117  */
118  $sql = "select upload_pk from upload where user_fk='$user_pk'";
119  $UploadResult = pg_query($PG_CONN, $sql);
120  DBCheckResult($UploadResult, $sql, __FILE__, __LINE__);
121  while($UploadRow = pg_fetch_assoc($UploadResult))
122  {
123  $upload_pk = $UploadRow['upload_pk'];
124  $sql = "select perm_upload_pk from perm_upload where group_fk='$group_pk' and upload_fk='$upload_pk'";
125  $PermUploadResult = pg_query($PG_CONN, $sql);
126  DBCheckResult($PermUploadResult, $sql, __FILE__, __LINE__);
127  if (pg_num_rows($PermUploadResult) == 0)
128  {
129  $perm_admin = Auth::PERM_ADMIN;
130  $sql = "insert into perm_upload(perm, upload_fk, group_fk) values($perm_admin, $upload_pk, $group_pk)";
131  $result = pg_query($PG_CONN, $sql);
132  DBCheckResult($result, $sql, __FILE__, __LINE__);
133  }
134  pg_free_result($PermUploadResult);
135  }
136  pg_free_result($UploadResult);
137 
138  }
139  pg_free_result($UserResult);
140 
141 
143  $sql = "delete from sysconfig where variablename = 'GlobalBrowse'";
144  $result = pg_query($PG_CONN, $sql);
145  DBCheckResult($result, $sql, __FILE__, __LINE__);
146  pg_free_result($result);
147 
149  $sql = "SELECT conname from pg_constraint where conname= 'license_ref_rf_shortname_key';";
150  $conresult = pg_query($PG_CONN, $sql);
151  DBCheckResult($conresult, $sql, __FILE__, __LINE__);
152  $tt = pg_num_rows($conresult);
153  if (pg_num_rows($conresult) == 0) {
154  $sql = "ALTER TABLE license_ref ADD CONSTRAINT license_ref_rf_shortname_key UNIQUE (rf_shortname); ";
155  $result = pg_query($PG_CONN, $sql);
156  DBCheckResult($result, $sql, __FILE__, __LINE__);
157  pg_free_result($result);
158  }
159  pg_free_result($conresult);
160 
162  $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;
163  DELETE FROM uploadtree_a WHERE uploadtree_pk IN (SELECT uploadtree_pk FROM uploadtree_a_upload WHERE upload_pk IS NULL);
164  DROP VIEW uploadtree_a_upload;";
165  $result = pg_query($PG_CONN, $sql);
166  DBCheckResult($result, $sql, __FILE__, __LINE__);
167  pg_free_result($result);
169  $sql = "SELECT conname from pg_constraint where conname= 'uploadtree_a_upload_fk_fkey';";
170  $conresult = pg_query($PG_CONN, $sql);
171  DBCheckResult($conresult, $sql, __FILE__, __LINE__);
172  if (pg_num_rows($conresult) == 0) {
173  $sql = "ALTER TABLE uploadtree_a ADD CONSTRAINT uploadtree_a_upload_fk_fkey FOREIGN KEY (upload_fk) REFERENCES upload (upload_pk) ON DELETE CASCADE; ";
174  $result = pg_query($PG_CONN, $sql);
175  DBCheckResult($result, $sql, __FILE__, __LINE__);
176  pg_free_result($result);
177  }
178  pg_free_result($conresult);
179 
181  global $MODDIR;
182  global $LIBEXECDIR;
183  $command = "$MODDIR/nomos/agent/nomos -V";
184  $version = exec($command, $out, $return_var);
185  if (0 == $return_var)
186  {
187  $pattern = '/r\‍((\w+)\‍)/';
188  preg_match($pattern, $version, $matches);
189 
190  $version230 = 6922;
191 
193  if (empty($matches[1]) || (5 > strlen($matches[1]) && $matches[1] <= $version230)) {
194  require_once("$LIBEXECDIR/fo_mapping_license.php");
195  print "Rename license in $LIBEXECDIR\n";
196  renameLicenses21to22($Verbose);
197  }
198  }
199 
201  if($Verbose)
202  print "Clear out the report cache.\n";
203  $sql = "DELETE FROM report_cache;";
204  $result = pg_query($PG_CONN, $sql);
205  DBCheckResult($result, $sql, __FILE__, __LINE__);
206  pg_free_result($result);
207 
208  return 0; // success
209 } // Migrate_21_22
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
DBCheckResult($result, $sql, $filenm, $lineno)
Check the postgres result for unexpected errors. If found, treat them as fatal.
Definition: common-db.php:187
Migrate_21_22($Verbose)
Create new groups, group_user_member, perm_upload and perm_folder records to support 2....
renameLicenses21to22($verbose)
Create map of old_shortname to new_shortname for 2.1 to 2.2 and call renameLicenses.
#define PERM_ADMIN
Administrator.
Definition: libfossology.h:34
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN