[Jifty-commit] r4016 - in apps/CASPlus/trunk: lib/CASPlus lib/CASPlus/Action lib/CASPlus/View t

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Thu Aug 30 18:14:30 EDT 2007


Author: sterling
Date: Thu Aug 30 18:14:30 2007
New Revision: 4016

Added:
   apps/CASPlus/trunk/lib/CASPlus/Action/CreateFirstUser.pm
   apps/CASPlus/trunk/t/20-action-CreateFirstUser.t
Modified:
   apps/CASPlus/trunk/   (props changed)
   apps/CASPlus/trunk/lib/CASPlus/CurrentUser.pm
   apps/CASPlus/trunk/lib/CASPlus/Dispatcher.pm
   apps/CASPlus/trunk/lib/CASPlus/View.pm
   apps/CASPlus/trunk/lib/CASPlus/View/Error.pm

Log:
 r11083 at riddle:  andrew | 2007-08-30 17:13:17 -0500
 Adding an admin/setup screen to help with installation.


Added: apps/CASPlus/trunk/lib/CASPlus/Action/CreateFirstUser.pm
==============================================================================
--- (empty file)
+++ apps/CASPlus/trunk/lib/CASPlus/Action/CreateFirstUser.pm	Thu Aug 30 18:14:30 2007
@@ -0,0 +1,140 @@
+use strict;
+use warnings;
+
+=head1 NAME
+
+CASPlus::Action::CreateFirstUser
+
+=cut
+
+package CASPlus::Action::CreateFirstUser;
+use base qw/CASPlus::Action Jifty::Action/;
+
+use Jifty::Param::Schema;
+use Jifty::Action schema {
+    param username =>
+        label is 'Admin Username',
+        is mandatory,
+        ajax validates,
+        ;
+
+    param password =>
+        label is 'Password',
+        is mandatory,
+        ajax validates,
+        render as 'Password',
+        ;
+
+    param confirm_password =>
+        label is 'Confirm Password',
+        hints is 'Please retype the password to make sure you got it right.',
+        is mandatory,
+        ajax validates,
+        render as 'Password',
+        ;
+};
+
+=head2 take_action
+
+=cut
+
+sub take_action {
+    my $self = shift;
+
+    my $user = CASPlus::Model::User->create(
+        username => $self->argument_value('username'),
+        password => $self->argument_value('password'),
+    );
+
+    use Data::Dumper;
+    $self->log->error(Dumper($user));
+
+    unless (defined $user and $user->id) {
+        my $msg = 'Failed to create the administrator account named "'
+                . $self->argument_value('username').'".';
+        $self->result->error($msg);
+        $self->log->fatal($msg);
+        return 0;
+    }
+
+    my $role = CASPlus::Model::Role->create(
+        name                       => 'Superuser',
+        may_manage_profiles        => 1,
+        may_manage_profile_objects => 1,
+        may_manage_roles           => 1,
+        may_manage_users           => 1,
+    );
+
+    unless (defined $role and $role->id) {
+        my $msg = 'Failed to create the Superuser role';
+        $self->result->error($msg);
+        $self->log->fatal($msg);
+        return 0;
+    }
+
+    my $role_member = CASPlus::Model::RoleMember->create(
+        the_role => $role,
+        the_user => $user,
+    );
+
+    unless (defined $role_member and $role_member->id) {
+        my $msg = 'Failed to assign the administation account "'
+                . $user->username . '" the Superuser role.';
+        $self->result->error($msg);
+        $self->log->fatal($msg);
+        return 0;
+    }
+
+    $self->report_success if not $self->result->failure;
+    
+    return 1;
+}
+
+=head2 report_success
+
+=cut
+
+sub report_success {
+    my $self = shift;
+
+    $self->result->message('Created the initial administrator account. You will now want to login.');
+    Jifty->web->next_page('/');
+}
+
+sub validate_password {
+    my $self = shift;
+    my $value = shift;
+
+    if (not defined $value or not $value) {
+        return $self->validation_error(
+            password => _('You must choose a password.'),
+        );
+    }
+
+    # XXX FIXME TODO At some point I should consider password policies and
+    # customization of them...
+
+    return $self->validation_ok('password');
+}
+
+sub validate_confirm_password {
+    my $self = shift;
+    my $value = shift;
+
+    if (not defined $value or not $value) {
+        return $self->validation_error(
+            confirm_password => _('You must confirm your password.')
+        );
+    }
+
+    if ($value ne $self->argument_value('password')) {
+        return $self->validation_error(
+            confirm_password => _('Password and confirmation do not match.')
+        );
+    }
+
+    return $self->validation_ok('password');
+}
+
+1;
+

Modified: apps/CASPlus/trunk/lib/CASPlus/CurrentUser.pm
==============================================================================
--- apps/CASPlus/trunk/lib/CASPlus/CurrentUser.pm	(original)
+++ apps/CASPlus/trunk/lib/CASPlus/CurrentUser.pm	Thu Aug 30 18:14:30 2007
@@ -312,7 +312,7 @@
 
     for my $role ($self->roles(1)) {
         for my $may_do_it (@admin_perms) {
-            return 1 if $role->may_do_it;
+            return 1 if $role->$may_do_it;
         }
     }
 

Modified: apps/CASPlus/trunk/lib/CASPlus/Dispatcher.pm
==============================================================================
--- apps/CASPlus/trunk/lib/CASPlus/Dispatcher.pm	(original)
+++ apps/CASPlus/trunk/lib/CASPlus/Dispatcher.pm	Thu Aug 30 18:14:30 2007
@@ -23,6 +23,23 @@
 
 =head2 before **
 
+This rule grants superuser access to the requester when there are no users.
+
+=cut
+
+before '**' => run {
+    my $users = CASPlus::Model::UserCollection->new;
+    $users->unlimit;
+    unless ($users->count) {
+        Jifty->web->current_user->is_superuser(1);
+    }
+    else {
+        Jifty->api->deny('CreateFirstUser');
+    }
+};
+
+=head2 before **
+
 This rule configures the menu items for the Administration page.
 
 =cut
@@ -404,6 +421,26 @@
 
 ];
 
+=head2 /admin/setup
+
+If there are no user accounts yet on the system. Going to this page will allow any user to create the first account. Otherwise, this returns an error.
+
+=cut
+
+on 'admin/setup' => run {
+    my $users = CASPlus::Model::UserCollection->new;
+    $users->unlimit;
+
+    if ($users->count) {
+        Jifty->log->error('Attempted access of CAS+ setup page. Access is denied.');
+        show '/error/already_setup';
+    }
+
+    my $action = Jifty->web->new_action(class => 'CreateFirstUser');
+    set action => $action;
+    show '/admin/setup';
+};
+
 =head2 /admin, /admin/**
 
 Only an administrator may see any of these pages. Sends the user an access denied message if they don't that access level.

Modified: apps/CASPlus/trunk/lib/CASPlus/View.pm
==============================================================================
--- apps/CASPlus/trunk/lib/CASPlus/View.pm	(original)
+++ apps/CASPlus/trunk/lib/CASPlus/View.pm	Thu Aug 30 18:14:30 2007
@@ -225,6 +225,19 @@
     CASPlus->admin_menu->render_as_admin_page;
 };
 
+template 'admin/setup' => page {
+    { title is 'Setup CAS+' };
+
+    p { _('From this page you can perform the initial configuration of your CAS+ server.'); };
+
+    p { _('Create the initial administrative user for your CAS+ system.'); };
+
+    form {
+        render_action get 'action';
+        form_submit label => 'Create Admin Account';
+    };
+};
+
 use CASPlus::View::Error;
 alias CASPlus::View::Error under '/error';
 

Modified: apps/CASPlus/trunk/lib/CASPlus/View/Error.pm
==============================================================================
--- apps/CASPlus/trunk/lib/CASPlus/View/Error.pm	(original)
+++ apps/CASPlus/trunk/lib/CASPlus/View/Error.pm	Thu Aug 30 18:14:30 2007
@@ -26,6 +26,12 @@
     }
 };
 
+template 'already_setup' => page {
+    { title is 'CAS+ Already Setup' };
+
+    p { _('This page is no longer accessible as CAS+ has already been configured. Attempted access to this page has been logged.'); };
+};
+
 template 'unknown' => page {
     { title is 'Unknown Error' };
 

Added: apps/CASPlus/trunk/t/20-action-CreateFirstUser.t
==============================================================================
--- (empty file)
+++ apps/CASPlus/trunk/t/20-action-CreateFirstUser.t	Thu Aug 30 18:14:30 2007
@@ -0,0 +1,15 @@
+#!/usr/bin/env perl
+use warnings;
+use strict;
+
+=head1 DESCRIPTION
+
+A (very) basic test harness for the CreateFirstUser action.
+
+=cut
+
+use Jifty::Test tests => 1;
+
+# Make sure we can load the action
+use_ok('CASPlus::Action::CreateFirstUser');
+


More information about the Jifty-commit mailing list