App-Dochazka-CLI

 view release on metacpan or  search on metacpan

lib/App/Dochazka/CLI/Commands/Employee.pm  view on Meta::CPAN


    # determine nick
    my $nick;
    if ( my $spec = $th->{'EMPLOYEE_SPEC'} ) {
        # other; just take whatever is after the '='
        ( $nick ) = $spec =~ m/=(.+)$/;
    } else {
        # self; get $nick from $current_emp
        $nick = $current_emp->nick;
    }

    # send the request 
    my $status = send_req( 'GET', "employee/nick/$nick/ldap" );
    return $status unless $status->ok;

    # success: spawn and populate object
    my $emp = App::Dochazka::Common::Model::Employee->spawn(
        %{ $status->payload }
    );

    my $message = "\n";
    $message .= "Nick:              " . $emp->nick . "\n";
    $message .= "LDAP full name:    " . ( $emp->fullname ? $emp->fullname : "(not set)" ) . "\n";
    $message .= "LDAP email:        " . ( $emp->email || "(not set)" ) . "\n";
    $message .= "LDAP secondary ID: " . ( $emp->sec_id ? $emp->sec_id : "(not set)" ) . "\n";
    
    if ( $current_priv eq 'admin' ) {
        # determine if employee already exists in Dochazka database
        my $status = send_req( 'GET', "employee/nick/" . $emp->nick . "/minimal" );
        if ( $status->level eq 'OK' and $status->code eq 'DOCHAZKA_EMPLOYEE_MINIMAL' ) {
            my $nick = $status->payload->{'nick'};
            my $eid = $status->payload->{'eid'};
            $message .= "\nEmployee $nick already exists in Dochazka with EID $eid\n";
        } else {
            my $nick = $emp->nick;
            $message .= "\nEmployee $nick is missing in Dochazka; to import, do \"EMPL=$nick LDAP IMPORT\"\n";
        }
    }

    return $CELL->status_ok( 'DOCHAZKA_CLI_NORMAL_COMPLETION', payload => $message );
}


=head3 employee_ldap_import

    EMPLOYEE_SPEC LDAP IMPORT

=cut

sub employee_ldap_import {
    print "Entering " . __PACKAGE__ . "::employee_ldap_import\n" if $debug_mode;
    my ( $ts, $th ) = @_;

    # parse test
    return parse_test( $ts, $th ) if $ts eq 'PARSE_TEST';

    # determine nick
    my ( $nick ) = $th->{'EMPLOYEE_SPEC'} =~ m/=(.+)$/;

    # send the request
    my $status = send_req( 'PUT', "employee/nick/$nick/ldap" );
    if ( $status->level eq 'OK' and $status->code eq 'DOCHAZKA_CUD_OK' ) {
        return employee_profile( $ts, $th );
    } else {
        return $status;
    }
}

    
=head3 employee_list

EMPLOYEE LIST
EMPLOYEE LIST _TERM

=cut

sub employee_list {
    my ( $ts, $th ) = @_;

    # parse test
    return parse_test( $ts, $th ) if $ts eq 'PARSE_TEST';

    my $priv;
    my $status = ( $priv = $th->{'_TERM'} )
        ? send_req( 'GET', "employee/list/$priv" )
        : send_req( 'GET', "employee/list" );

    $priv = $priv || 'all';

    return $status unless $status->ok;
    return $CELL->status_ok( 'DOCHAZKA_CLI_NORMAL_COMPLETION',
        payload => "\nList of employees with priv level ->$priv<-\n    " .
                   join( "\n    ", @{ $status->payload } ) .  "\n" );
}


=head3 employee_team

EMPLOYEE TEAM

=cut

sub employee_team {
    my ( $ts, $th ) = @_;
    $log->debug( "Entering " . __PACKAGE__ . "::employee_team" );

    # parse test
    return parse_test( $ts, $th ) if $ts eq 'PARSE_TEST';

    # determine employee
    my $status = determine_employee( $th->{'EMPLOYEE_SPEC'} );
    return $status unless $status->ok;
    my $emp = $status->payload;
    my $eid = $emp->eid;
    my $nick = $emp->nick;

    $status = ( $eid == $current_emp->eid )
        ? send_req( 'GET', "employee/team" )
        : send_req( 'GET', "employee/eid/$eid/team" );
    return $status unless $status->ok;

lib/App/Dochazka/CLI/Commands/Employee.pm  view on Meta::CPAN

    my $emp_obj;
    if ( my $e_spec = $PROPLIST{'emp_spec'} ) {
        $status = determine_employee( $e_spec );
        return $status unless $status->ok;
        $emp_obj = $status->payload;
    } elsif ( $emp_obj = $PROPLIST{'emp_obj'} ) {
    } else {
        die "AAAAAAAAAAAAAHHHHH!";
    }
    my $eid = $emp_obj->eid;
    my $prop = $PROPLIST{'prop'};
    my $val = $PROPLIST{'val'};
    $val =~ s/['"]//g;
    $status = send_req( 'POST', "employee/eid", <<"EOS" );
{ "eid" : $eid, "$prop" : "$val" }
EOS
    return rest_error( $status, "Modify employee profile" ) unless $status->ok;

    my $message = "Profile of employee " . $emp_obj->nick . 
        " has been modified ($prop -> $val)\n";

    return $CELL->status_ok( 'DOCHAZKA_CLI_NORMAL_COMPLETION', payload => $message );
}


=head3 _set_password

Takes PARAMHASH with following properties:

     eid => EID of employee
     password => the new password (*optional*)

=cut

sub _set_password {
    my %PH = @_;
    my $eid = $PH{'eid'};
    my $newpass = $PH{'password'};

    print "It is important that the new password really be what you intended.\n";
    print "Therefore, we are going to ask you to enter the desired password\n";
    print "twice, so you have a chance to double-check. ";
    print "\n\n";

    # prompt for new password and ask nicely for confirmation
    if ( ! $newpass ) {
        ReadMode ('noecho');
        print "New password      : ";
        chomp( $newpass = <> );
        ReadMode ('restore');
        print "\n";
    }
    ReadMode ('noecho');
    print "New password again: ";
    chomp( my $confirm = <> );
    ReadMode ('restore');
    print "\n";
    return $CELL->status_err( 'DOCHAZKA_CLI_NO_MATCH' ) unless $newpass eq $confirm;

    # send REST request
    my $status = send_req( 'PUT', "employee/eid/$eid", <<"EOS" );
{ "password" : "$newpass" }
EOS

    return $status unless $status->ok;
    return $CELL->status_ok( 'DOCHAZKA_CLI_NORMAL_COMPLETION', 
        payload => "Password changed" );
}


=head3 _display_employee_ok

Given an employee object, prepare OK return status intended for EMPLOYEE PROFILE
but usable also for other commands.

=cut

sub _display_employee_ok {
    my ( $emp ) = @_;

    # determine supervisor
    my $supervisor = determine_supervisor( $emp );

    my $message = "\n";
    $message .= "Full name:    " . ( $emp->fullname ? $emp->fullname : "(not set)" ) . "\n";
    $message .= "Nick:         " . $emp->nick . "\n";
    $message .= "Email:        " . ( $emp->email || "(not set)" ) . "\n";
    $message .= "Workforce ID: " . ( $emp->sec_id ? $emp->sec_id : "(not set)" ) . "\n";
    $message .= "Reports to:   " . ( $supervisor->nick || "(not set)" ) . "\n";

    return $CELL->status_ok( 'DOCHAZKA_CLI_NORMAL_COMPLETION', payload => $message );
}


1;



( run in 2.057 seconds using v1.01-cache-2.11-cpan-ceb78f64989 )