(redirected from WikiPatches/LoginRework)

[Home]LoginRework

UseModWiki | RecentChanges | Preferences

Description

This adds the obvious (to me) login/logout support that a wiki needs. I wanted it to be non restrictive, but also so that everyone using it knew who was doing what. I know it is critical to make edits easy, but if you do this once then the cookie holds the name and the login is easy after that.

The main menu does not have Preferences in it until you login. I have tried to keep Numeric and User Name login operational at the same time. Passwords are allowed to be empty. If you try to edit then you will be asked to login. Login page allows for a New Login to be created.

Most of the code came from these two patches:


Comments

This does not work for usernames with spaces in them. If your wiki allows free links, login will will fail for those who do this - I removed the "all usernames must be CamelCase" note, as the wiki I'm running actually has CamelCase links turned off. I'm trying to decide if I'm insane enough to fix this!
-[Twin] - DeadLink, administrix of the [Puzzlebox Background Data wiki] - DeadLink.

The Patch

--- wiki.1.0.0.pl	Thu Sep 12 10:53:14 2002
+++ wikipatch.LoginRework.pl	Thu Jan 15 17:04:44 2004
@@ -1212,7 +1212,23 @@
 }
 
 sub GetPrefsLink {
-  return &ScriptLink("action=editprefs", T('Preferences'));
+  if ($UserID > 400) {
+      return " | " . &ScriptLink("action=editprefs", T('Preferences'));
+  }
+  return "";
+}
+
+sub GetLoginLink {
+  my $result = '';
+  my $userName = &GetParam("username","");
+  if ($UserID <= 400) {
+    $result = " | " . &ScriptLink("action=login", T('Login'));  
+  } else {
+    if (!($userName =~ /^[0-9]+$/)) {
+      $result = " | " . &GetPageLink($userName, Ts('%s' , $userName));
+    }
+  }
+  return $result;
 }
 
 sub GetRandomLink {
@@ -1487,6 +1503,7 @@
   if (&GetParam("linkrandom", 0)) {
     $bartext .= " | " . &GetRandomLink();
   }
+  $bartext .= &GetLoginLink(); 
   if ($UserGotoBar ne '') {
     $bartext .= " | " . $UserGotoBar;
   }
@@ -2576,6 +2593,22 @@
   }
 }
 
+sub LoadUserNameData {
+  my ($username) = @_;
+  my ($data, $status, $userfile);
+
+  %UserData = ();
+  $userfile = &UserNameDataFilename($username);
+  chomp $userfile;                                           
+  ($status, $data) = &ReadFile($userfile);
+  if (!$status) {
+    $UserID = 112;  # Could not open file.  Consider warning message?
+    return;
+  }
+  %UserData = split(/$FS1/, $data, -1);  # -1 keeps trailing null fields
+  $UserID = $UserData{'id'};
+}
+
 sub LoadUserData {
   my ($data, $status);
 
@@ -2680,6 +2713,13 @@
     return 1  if (&UserIsEditor());
     return 0  if (&UserIsBanned());
   }
+  if ($id =~ m/(.+)\/.+/) { # if a subpage.
+    if (UserNameDataFilename($1)) { # if the main page is a user id
+      if ($1 ne &GetParam("username", "")) { # if not the current user
+        return 0;
+      }
+    }
+  }
   return 1;
 }
 
@@ -3111,9 +3151,10 @@
       &DoEditLinks();
     } elsif ($action eq "login") {
       &DoEnterLogin();
+    } elsif ($action eq "logout") {
+      &DoLogout();
     } elsif ($action eq "newlogin") {
-      $UserID = 0;
-      &DoEditPrefs();  # Also creates new ID
+      &DoNewLogin();  # Also creates new ID
     } elsif ($action eq "version") {
       &DoShowVersion();
     } elsif ($action eq "rss") {
@@ -3145,6 +3186,10 @@
     &DoLogin();
     return;
   }
+  if (&GetParam("new_login", 0)) {
+    &DoSaveLogin();
+    return;
+  }
   if (&GetParam("edit_links", 0)) {
     &DoUpdateLinks();
     return;
@@ -3178,6 +3223,12 @@
   my ($header, $editRows, $editCols, $userName, $revision, $oldText);
   my ($summary, $isEdit, $pageTime);
 
+  if( &GetParam("username","") eq "" ) {
+    print &GetHeader("", T('Editing Denied'), "");
+    print Ts('Editing not allowed without a username. Please Login.', $SiteName);
+    print &GetCommonFooter();
+    return;
+  }
   if ($FreeLinks) {
     $id = &FreeToNormal($id);  # Take care of users like Markus Lude :-)
   }
@@ -3271,13 +3322,6 @@
     print T($EditNote) . '<br>';  # Allow translation
   }
   print $q->submit(-name=>'Save', -value=>T('Save')), "\n";
-  $userName = &GetParam("username", "");
-  if ($userName ne "") {
-    print ' (', T('Your user name is'), ' ',
-          &GetPageLink($userName) . ') ';
-  } else {
-    print ' (', Ts('Visit %s to set your user name.', &GetPrefsLink()), ') ';
-  }
   print $q->submit(-name=>'Preview', -value=>T('Preview')), "\n";
   if ($isConflict) {
     print "\n<br><hr><p><strong>", T('This is the text you submitted:'),
@@ -3326,7 +3370,7 @@
 
   $recentName = $RCName;
   $recentName =~ s/_/ /g;
-  &DoNewLogin()  if ($UserID < 400);
+  return &DoLogin() if ($UserID < 400);
   print &GetHeader('', T('Editing Preferences'), "");
   print '<div class=wikipref>';
   print &GetFormStart();
@@ -3428,6 +3472,21 @@
                       -label=>$label);
 }
 
+sub UserNameDataFilename {
+  my ($id) = @_;
+  my ($userfile, $status, $data, %userdata_tmp);
+
+  foreach $userfile (glob("$UserDir/*/*.db")) {
+    ($status, $data) = &ReadFile($userfile);
+    if ($status) {
+      %userdata_tmp = split(/$FS1/, $data, -1);
+      if ($userdata_tmp{'username'} eq $id) {
+        return $userfile;
+      }
+    }
+  }
+}
+
 sub DoUpdatePrefs {
   my ($username, $password, $stylesheet);
 
@@ -3452,26 +3511,31 @@
     $username =  &FreeToNormal($username);
     $username =~ s/_/ /g;
   }
-  if ($username eq "") {
-    print T('UserName removed.'), '<br>';
-    undef $UserData{'username'};
-  } elsif ((!$FreeLinks) && (!($username =~ /^$LinkPattern$/))) {
-    print Ts('Invalid UserName %s: not saved.', $username), "<br>\n";
-  } elsif ($FreeLinks && (!($username =~ /^$FreeLinkPattern$/))) {
-    print Ts('Invalid UserName %s: not saved.', $username), "<br>\n";
-  } elsif (length($username) > 50) {  # Too long
-    print T('UserName must be 50 characters or less. (not saved)'), "<br>\n";
-  } else {
-    print Ts('UserName %s saved.', $username), '<br>';
-    $UserData{'username'} = $username;
+  if ($username ne $UserData{'username'}) {
+    my $userfile = UserNameDataFilename($username);
+    chomp $userfile;
+    if ($userfile ne '') {
+      print Ts('Duplicate Username %s: not saved.', $username), "<br>\n";
+    } elsif ((!$FreeLinks) && (!($username =~ /^$LinkPattern$/))) {
+      print Ts('Invalid UserName %s: not saved.', $username), "<br>\n";
+    } elsif ($FreeLinks && (!($username =~ /^$FreeLinkPattern$/))) {
+      print Ts('Invalid UserName %s: not saved.', $username), "<br>\n";
+    } elsif (length($username) > 50) {  # Too long
+      print T('UserName must be 50 characters or less. (not saved)'), "<br>\n";
+    } else {
+      print Ts('UserName %s saved.', $username), '<br>';
+      $UserData{'username'} = $username;
+    }
   }
   $password = &GetParam("p_password",  "");
-  if ($password eq "") {
-    print T('Password removed.'), '<br>';
-    undef $UserData{'password'};
-  } elsif ($password ne "*") {
-    print T('Password changed.'), '<br>';
-    $UserData{'password'} = $password;
+  if (($password ne "*") and ($password ne $UserData{'password'})) {
+    if ($password eq "") {
+      print T('Password removed.'), '<br>';
+      undef $UserData{'password'};
+    } else  {
+      print T('Password changed.'), '<br>';
+      $UserData{'password'} = $password;
+    }
   }
   if (($AdminPass ne "") || ($EditPass ne "")) {
     $password = &GetParam("p_adminpw",  "");
@@ -3599,6 +3663,22 @@
   print &GetCommonFooter();
 }
 
+sub DoLogout {
+  %SetCookie = ();
+  $TimeZoneOffset = 0;
+  undef $q->{'.cookies'};  # Clear cache if it exists (for SpeedyCGI)
+  $UserID = 111;
+  %UserData = ();   
+
+  $SetCookie{'id'} = 0;
+  $SetCookie{'randkey'} = 0;
+  $SetCookie{'rev'} = 1;
+
+  print &GetHeader('', &QuoteHtml(T('User logged out')), '');
+  print " <p>You were successfully logged out.<p>";
+  print &GetMinimumFooter();
+}
+
 # Create a new user file/cookie pair
 sub DoNewLogin {
   # Consider warning if cookie already exists
@@ -3614,16 +3694,65 @@
   $UserData{'createtime'} = $Now;
   $UserData{'createip'} = $ENV{REMOTE_ADDR};
   &SaveUserData();
+  $UserData{'username'} = $UserID;
+
+  print &GetHeader('', T('New Login'), "");
+  print &GetFormStart();
+  print GetHiddenValue("new_login", 1), "\n";
+
+  print Ts('Your new User ID number: %s', $UserID) . "<br>\n";
+  print '<br>', T('User Name :'), ' ',
+        $q->textfield(-name=>'p_username', -value=>'', 
+                           -size=>15, -maxlength=>50),
+          ' ', T('(Wikis uses real names in CamelCase. EG. FredDagg.)');
+  print '<br>', T('Password  :'), ' ',
+        $q->password_field(-name=>'p_password', -value=>'', 
+                           -size=>15, -maxlength=>50),
+          ' ', T('(Passwords are completely optional.)');
+  print '<br>', $q->submit(-name=>'NewLogin', -value=>T('New Login')), "\n";
+  print $q->endform;
+  print &GetMinimumFooter();
+}
+
+sub DoSaveLogin {
+  my ($username, $password, $stylesheet, $userfile, $result);
+
+  $username = &GetParam("p_username",  "");
+  $password = &GetParam("p_password",  "");
+  if ($FreeLinks) {
+    $username =~ s/^\[\[(.+)\]\]/$1/;  # Remove [[ and ]] if added
+    $username =  &FreeToNormal($username);
+    $username =~ s/_/ /g;
+  }
+  $userfile = UserNameDataFilename($username);
+  chomp $userfile;
+  if ($userfile ne '') {
+    $result = Ts('Duplicate Username %s: not saved.', $username), "<br>\n";
+  } elsif ((!$FreeLinks) && (!($username =~ /^$LinkPattern$/))) {
+    $result = Ts('Invalid UserName %s: not saved.', $username), "<br>\n";
+  } elsif ($FreeLinks && (!($username =~ /^$FreeLinkPattern$/))) {
+    $result = Ts('Invalid UserName %s: not saved.', $username), "<br>\n";
+  } elsif (length($username) > 50) {  # Too long
+    $result = T('UserName must be 50 characters or less. (not saved)'), "<br>\n";
+  } else {
+    $result = Ts('New login UserName %s saved.', $username), '<br>';
+    $UserData{'username'} = $username;
+    $UserData{'password'} = $password;
+    &SaveUserData();
+  }
+  print &GetHeader('',T('Creating New Login User Name'), '');
+  print '<br>';
+  print $result;
+  print &GetMinimumFooter();
 }
 
 sub DoEnterLogin {
   print &GetHeader('', T('Login'), "");
   print &GetFormStart();
   print &GetHiddenValue('enter_login', 1), "\n";
-  print '<br>', T('User ID number:'), ' ',
-        $q->textfield(-name=>'p_userid', -value=>'',
-                      -size=>15, -maxlength=>50);
-  print '<br>', T('Password:'), ' ',
+  print Ts("If you don't have a User Name yet, go to %s and get one.", &ScriptLink("action=newlogin",'Create new login')), "<br>\n";
+  print '<br>', T('User Name :'), ' ', &GetFormText('userid', "", 15, 50);
+  print '<br>', T('Password  :'), ' ',
         $q->password_field(-name=>'p_password', -value=>'', 
                            -size=>15, -maxlength=>50);
   print '<br>', $q->submit(-name=>'Login', -value=>T('Login')), "\n";
@@ -3640,7 +3769,7 @@
   $uid = &GetParam("p_userid", "");
   $uid =~ s/\D//g;
   $password = &GetParam("p_password",  "");
-  if (($uid > 199) && ($password ne "") && ($password ne "*")) {
+  if ($uid > 199) {
     $UserID = $uid;
     &LoadUserData();
     if ($UserID > 199) {
@@ -3650,6 +3779,22 @@
         $SetCookie{'randkey'} = $UserData{'randkey'};
         $SetCookie{'rev'} = 1;
         $success = 1;
+      }
+    }
+  }
+  if (!$success) { # Try a UserName Login.
+    %UserData = ();
+    my $username = &GetParam("p_userid", "");
+    if (!($username =~ /^[0-9]+$/)) {
+      &LoadUserNameData($username);
+      if ($UserID > 199) {
+        if (defined($UserData{'password'}) &&
+            ($UserData{'password'} eq $password)) {
+          $SetCookie{'id'} = $UserID;
+          $SetCookie{'randkey'} = $UserData{'randkey'};
+          $SetCookie{'rev'} = 1;
+          $success = 1;
+        }
       }
     }
   }
--- Proposal:
- print &GetHeader('', &QuoteHtml(T('User logged out')), '');
+ print &GetHeader('', T('User logged out'), '');
--JuanmaMP

UseModWiki | RecentChanges | Preferences
Edit text of this page | View other revisions | Search MetaWiki
Last edited February 6, 2011 7:12 pm by JuanmaMP (diff)
Search: