Begin modifying UserCake
authorSam Moore <[email protected]>
Sun, 20 Oct 2013 13:35:12 +0000 (21:35 +0800)
committerSam Moore <[email protected]>
Sun, 20 Oct 2013 13:35:12 +0000 (21:35 +0800)
Create admin upload page for uploading / purging users. Incomplete.
Note that this page needs to be secured somehow.

Realised that we probably need to add the UserCake install scripts as well, so we can
customise the database and other things when it is installed.

Modified something in the server too apparently.
I love it when I forget what I actually did...

server/login.c
server/parameters
testing/MCTXWeb/public_html/users/admin_upload_users.php
testing/MCTXWeb/public_html/users/models/config.php
testing/MCTXWeb/public_html/users/models/db-settings.php
testing/MCTXWeb/public_html/users/models/funcs.php

index c1d981f..30105cb 100644 (file)
@@ -110,7 +110,12 @@ UserType Login_MySQL(const char * user, const char * pass,
                Log(LOGERR, "No user matching %s", user);
        }
 
-
+       //TODO: Handle administrator users somehow better than this
+       // UserCake stores the permission level in a seperate table to the username/password, which is annoying
+       if (user_type != USER_UNAUTH && strcmp(user, "admin") == 0)
+       {
+               user_type = USER_ADMIN;
+       }
        mysql_free_result(result);
        mysql_close(con);
        return user_type;
@@ -367,7 +372,7 @@ void Login_Handler(FCGIContext * context, char * params)
                {
                        //WARNING: C string manipulation code approaching!
                        // Non reentrent; uses strsep and modifies g_options.auth_options
-                       // If problems happen, try strdup ...
+                       // If problems happen, try strdup first ...
                        static char * db_opts[] = {"root", "", "users", "uc_users"};
                        static bool db_init_opts = false;
                        if (!db_init_opts)
@@ -386,7 +391,7 @@ void Login_Handler(FCGIContext * context, char * params)
                                                break;
                                        }
                                }                       
-                               Log(LOGDEBUG, "MySQL: user %s pass %s name %s table %s", db_opts[0], db_opts[1], db_opts[2], db_opts[3]);       
+                               //Log(LOGDEBUG, "MySQL: user %s pass %s name %s table %s", db_opts[0], db_opts[1], db_opts[2], db_opts[3]);     
                        }
 
                        user_type = Login_MySQL(user, pass, g_options.auth_uri, db_opts[0],db_opts[1], db_opts[2], db_opts[3]);
index 99b3f7e..5fdc595 100644 (file)
@@ -23,9 +23,9 @@ pin_test="0"
 # Set to the URI to use authentication
 #auth_uri="ldap://192.168.1.1#ou=People,dc=daedalus"
 #auth_uri="ldaps://ldap.pheme.uwa.edu.au#ou=Users,ou=UWA,dc=uwads,dc=uwa,dc=edu,dc=au" #UWA
-auth_uri="/etc/shadow"
+#auth_uri="/etc/shadow"
 #auth_uri="shadow"
-#auth_uri="mysql://localhost#root,$(cat mysql_password)"
+auth_uri="mysql://localhost#root,$(cat mysql_password)"
 
 
 ## OPTIONS TO BE PASSED TO SERVER; DO NOT EDIT
index d068f71..53c71ce 100644 (file)
 <?php
 
+/**
+ * This file is not part of the original UserCake system, but uses it.
+ */
+
 require_once("models/config.php");
 if (!securePage($_SERVER['PHP_SELF'])){die();}
 
 require_once("models/header.php");
-createPage("User Upload");
+
+$text_area="# Rows starting with '#' are ignored. Rows are of the form:\n# username, Full Name, email";
 
 
+$show_form = true;
 if (!empty($_POST))
 {
-  echo "<p> Uploaded! </p>";
+
+  $current_users = fetchAllUsers();
+
+  // Check form action
+  if ($_POST['action'] === "Download") // Download list of users and populate the text area
+  {
+  
+    foreach ($current_users as $u)
+    {
+      if ($u['user_name'] !== "admin")
+        $text_area=$text_area."\n".$u['user_name'].",".$u['display_name'].",".$u['email'];
+    }
+  }
+  else if ($_POST['action'] === "Upload") // Upload users in the text area
+  {
+
+    $text_area = $_POST['userUpload'];
+
+
+    // Iterate through each row
+    $all_rows=preg_split("/((\r?\n)|(\r\n?))/", $_POST['userUpload']);
+   
+    
+    
+    
+    foreach ($all_rows as $row)
+    {
+      if (empty($row) or $row[0] === '#')
+        continue;
+
+      $errors = array();
+
+      $fields = preg_split("/,/",  $row);
+     
+      $user_name = trim($fields[0]);
+      $display_name = trim($fields[1]);
+      $email = trim($fields[2]);
+      // generate the temporary password
+      $password = generatePassword();
+
+
+      if(count($errors) == 0)
+      {        
+        //Construct a user object
+               $user = new User($username,$displayname,$password,$email);
+               
+               //Checking this flag tells us whether there were any errors such as possible data duplication occured
+               if(!$user->status)
+               {
+                       if($user->username_taken) $errors[] = lang("ACCOUNT_USERNAME_IN_USE",array($username));
+                       if($user->displayname_taken) $errors[] = lang("ACCOUNT_DISPLAYNAME_IN_USE",array($displayname));
+                       if($user->email_taken)    $errors[] = lang("ACCOUNT_EMAIL_IN_USE",array($email));               
+               }
+               else
+               {
+                       //Attempt to add the user to the database, carry out finishing  tasks like emailing the user (if required)
+                       if(!$user->userCakeAddUser())
+                       {
+                               if($user->mail_failure) $errors[] = lang("MAIL_ERROR");
+                               if($user->sql_failure)  $errors[] = lang("SQL_ERROR");
+                       }
+               }
+      }
+      if(count($errors) == 0)
+      {
+               $successes[] = $user->success;
+      }
+
+      echo resultBlock($errors,$successes);
+    }
+
+    
+    
+    
+  
+  }
+
+
 }
-else
+
+if ($show_form)
 {
+  /* I can't get fucking file uploads to fucking work with fucking nginx
   echo "<p> Please provide a CSV file of usernames and email addresses. </p>
+  <p> Click <a href=\"upload_users_example.csv\">here</a> for an example file. </p>
   <div class=\"title\">Upload</div>
-  <form name='newUser' action='".$_SERVER['PHP_SELF']."' method='post'>
-  <input type=\"file\" name=\"users\"/>
+  <form  action=\"".$_SERVER['PHP_SELF']."\" enctype=\"multipart/form-data\" method=\"post\">
+  <input type=\"file\" name=\"userUpload\"/>
   <input type=\"submit\" value=\"Upload\"/>
   </form>";
+  */
+  echo "
+  <form action=\"".$_SERVER['PHP_SELF']."\" method=\"POST\">
+  <p> Action to take on adding users: </p>
+  <p> <input type=\"radio\" name=\"upload_mode\" value=\"keep\" checked/>Keep existing users and add these users</p>
+  <p> <input type=\"radio\" name=\"upload_mode\" value=\"purge\"/>Purge existing users and add these users</p>
+  <input type=\"submit\" name=\"action\" value=\"Upload\"/> 
+  <input type=\"submit\" name=\"action\" value=\"Download\"/>
+  <input type=\"submit\" name=\"action\" value=\"Reset\"/>
+  <p> Enter or copy/paste user information below (resize the text area if necessary): </p>
+  <p>
+  <textarea name=\"userUpload\" rows=\"50\" cols=\"100\">".$text_area."</textarea> </p>
+
+  </form>";
 }
   
 ?> 
index 6f2f3f2..2f7c635 100644 (file)
@@ -20,7 +20,7 @@ $stmt->close();
 $emailActivation = $settings['activation']['value'];\r
 $mail_templates_dir = "models/mail-templates/";\r
 $websiteName = $settings['website_name']['value'];\r
-$websiteUrl = $settings['website_url']['value'];\r
+$websiteUrl = $settings['website_url']['value']."/users";\r
 $emailAddress = $settings['email']['value'];\r
 $resend_activation_threshold = $settings['resend_activation_threshold']['value'];\r
 $emailDate = date('dmy');\r
index 0df3b94..6d16d17 100644 (file)
@@ -8,9 +8,11 @@ http://usercake.com
 $db_host = "localhost"; //Host address (most likely localhost)\r
 $db_name = "users"; //Name of Database\r
 $db_user = "root"; //Name of database user\r
-$db_pass = file_get_contents("mysql_password"); // TODO: Create that file or replace this line.\r
+$db_pass = trim(file_get_contents("mysql_password"));// TODO: Create that file or replace this line.\r
 $db_table_prefix = "uc_";\r
 \r
+\r
+\r
 GLOBAL $errors;\r
 GLOBAL $successes;\r
 \r
index 07af426..b1afc72 100644 (file)
@@ -80,6 +80,16 @@ function generateHash($plainText, $salt = null)
        return crypt($plainText, $salt);\r
 }\r
 \r
+/**\r
+ * Generates a random password for emailing to new users.\r
+ * User should be asked to change the password.\r
+ */\r
+function generatePassword()\r
+{\r
+  $random = file_get_contents("/dev/urandom", false, null, 0, 25);\r
+  return bin2hex($random);\r
+}\r
+\r
 //Checks if an email is valid\r
 function isValidEmail($email)\r
 {\r

UCC git Repository :: git.ucc.asn.au