[interchange-cvs] interchange - jon modified code/UI_Tag/su.coretag

interchange-core@icdevgroup.org interchange-core@icdevgroup.org
Tue Aug 6 15:53:00 2002


User:      jon
Date:      2002-08-06 19:52:30 GMT
Modified:  code/UI_Tag su.coretag
Log:
Add new option to su usertag, 'create_user', which allows combined
creation of new user and switching to that user. Any options you would
pass to userdb tag can be given to su usertag, to tweak account creation.
An example:

[su create_user=3D1 username=3Dbobby password=3Dhowdy verify=3Dhowdy]

If user already exists, it's silently switched to.

Revision  Changes    Path
1.4       +46 -20    interchange/code/UI_Tag/su.coretag


rev 1.4, prev_rev 1.3
Index: su.coretag
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /var/cvs/interchange/code/UI_Tag/su.coretag,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -u -r1.3 -r1.4
--- su.coretag	16 Feb 2002 09:39:08 -0000	1.3
+++ su.coretag	6 Aug 2002 19:52:30 -0000	1.4
@@ -7,8 +7,9 @@
 	my ($user, $opt) =3D @_;
 	use vars qw/$Session $Tag $ready_safe $Scratch/;
=20
-	my $super  =3D $Tag->if_mm('super');
-	my $former =3D $Vend::username;
+	# Note: If adding any new %$opt keys, make sure to also add them to
+	# the list of options to be stripped before passing the remainder
+	# to tag userdb; search below for $new_user.
=20
 	$opt->{profile} =3D 'ui'     if $opt->{admin} and ! $opt->{profile};
=20
@@ -22,12 +23,22 @@
=20
 	if(! $u) {
 		my $place =3D $opt->{profile} || 'default';
-		::logError("Can't find UserDB repository, profile '%'", $place);
+		::logError("Can't find UserDB repository, profile '%s'", $place);
 		return undef;
 	}
-	my $table  =3D $u->{database};
-	my $ufield =3D $u->{user_field};
+	my $table  =3D $u->{database} || 'userdb';
+	my $ufield =3D $u->{user_field} || 'username';
 	my $going_to_admin =3D $u->{admin} || $opt->{admin};
+#::logDebug("user table=3D$table ufield=3D$ufield");
+
+	if ($opt->{create_user}) {
+		# these settings must be done before any access to the table
+		$Vend::WriteDatabase{$table} =3D 1;
+		$Vend::Cfg->{DynamicData}{$table} =3D 1;
+	}
+
+	my $super  =3D $Tag->if_mm('super');
+	my $former =3D $Vend::username;
=20
 	if($user and $going_to_admin and ! $super) {
 		::logError("attempt to su to admin user %s by non-super user %s",
@@ -44,7 +55,6 @@
 		return undef;
 	}
=20
-
 	my $dir =3D "$Global::ConfDir/tmp";
 	if (! -d $dir) {
 		if(-e $dir) {
@@ -97,16 +107,17 @@
 		return;
 	}
 	elsif ($user) {
-		if( $u->{admin} and ! $super) {
-		}
-#::logDebug("user table=3D$table ufield=3D$ufield");
-		$table  ||=3D 'userdb';
-		$ufield ||=3D 'username';
-#::logDebug("user table=3D$table ufield=3D$ufield");
+		my $new_user;
 		if(! $Tag->data($table, $ufield, $user) ) {
-			$Scratch->{ui_error} =3D errmsg("attempt to su to non-existent user %s"=
, $user);
-			return undef;
+			if ($opt->{create_user}) {
+				$new_user =3D 1;
+			}
+			else {
+				$Scratch->{ui_error} =3D errmsg("attempt to su to non-existent user %s=
", $user);
+				return undef;
+			}
 		}
+
 		my $rand	=3D random_string();
 		my $sess	=3D uneval_it($Session);
 #::logDebug("sess is $sess");
@@ -126,13 +137,28 @@
 		Vend::Session::init_session();
 		$Session =3D $Vend::Session;
=20
-		$Vend::username =3D $Session->{username} =3D $user;
-		$Vend::admin    =3D $Session->{admin}    =3D $going_to_admin;
-
-		$Session->{su} =3D $sess;
-		$Session->{logged_in} =3D 1;
+		if ($new_user) {
+			# pass on any non-su options to userdb tag
+			my $newopt =3D { %$opt };
+			delete @{$newopt}{qw( admin exit create_user )};
+			$newopt->{username} =3D $user;
+			my $result =3D $Tag->userdb('new_account', $newopt);
+			unless ($result) {
+				my $error =3D errmsg("Failed to create new user '%s' in su tag", $user=
);
+				logError($error);
+				$Scratch->{ui_error} =3D $error;
+				return undef;
+			}
+			$Session->{su} =3D $sess;
+		}
+		else {
+			$Vend::username =3D $Session->{username} =3D $user;
+			$Vend::admin    =3D $Session->{admin}    =3D $going_to_admin;
+			$Session->{logged_in} =3D 1;
+			$Session->{su} =3D $sess;
+			$Tag->userdb('load');
+		}
=20
-		$Tag->userdb('load');
 		my $dest =3D $Tag->if_mm('logged_in') ? 'admin user' : 'regular user';
 		logError(
 			"superuser %s switched user to %s %s",