Solstice::AuthZ::Role - Models a group of allowed/disallowed actions.


Solstice documentation Contained in the Solstice distribution.

Index


Code Index:

NAME

Top

Solstice::AuthZ::Role - Models a group of allowed/disallowed actions.

SYNOPSIS

Top

  use Action;

  my $model = Action->new();
  # The following accessors were created auto-magically by majere...

  my $obj = $model->getName();
  $model->setName($obj);

  my $obj = $model->getDescription();
  $model->setDescription($obj);

  my $obj = $model->getAppID();
  $model->setAppID($obj);

DESCRIPTION

Top

Represents an Authz Action record.

Superclass

Solstice::Model (Solstice::Model)

Export

No symbols exported.

Methods

new()

Constructor.

getName()

An accessor for _name.

setName($obj)

An accessor for _name.

getSTID()

An accessor for _name.

setSTID($obj)

An accessor for _name.

addAction

adds an action to the list of allowed permissions =cut

Private Methods

_init($id)

Sets up the object

Modules Used

Solstice::AuthZ::Action, Solstice::Database, Solstice::Model (Solstice::Model).

AUTHOR

Top

Catalyst Group, <catalyst@u.washington.edu>

VERSION

Top

$Revision: 3384 $

COPYRIGHT

Top


Solstice documentation Contained in the Solstice distribution.
package Solstice::AuthZ::Role;

# $Id: Role.pm 3384 2006-05-17 21:57:05Z mcrawfor $

use 5.006_000;
use strict;
use warnings;

use Solstice::AuthZ::Action;
use Solstice::Database;
use Solstice::Model;
use Solstice::AuthZ::Factory;

our @ISA = qw(Solstice::Model);

our ($VERSION) = ('$Revision: 3384 $' =~ /^\$Revision:\s*([\d.]*)/);

sub new {
    my $obj = shift;
    my $input = shift;

    my $self = $obj->SUPER::new(@_);

    $self->setSTID(0);
    $self->{_actions} = [];

    if($self->isValidHashRef($input)){
        $self->_initFromHashRef($input);
    }elsif(defined $input){
        $self->_init($input);
    }

    return $self;
}


sub getID{
    my $self = shift;
    return $self->{_id};
}

sub setID{
    my $self = shift;
    $self->{_id} = shift;
}

sub getName {
    my $self = shift;
    return $self->{_name};
}

sub setName {
    my $self = shift;
    $self->{_name} = shift;
}

sub getSTID {
    my $self = shift;
    return $self->{_stid};
}

sub setSTID {
    my $self = shift;
    $self->{_stid} = shift;
}

sub addActions{
    my $self = shift;
    my @actions = @_;

    push @{$self->{_actions}}, @actions;
}

sub getActions{
    my $self = shift;
    wantarray ? return @{$self->{_actions}} : return $self->{_actions};
}

sub clearActions{
    my $self = shift;
    $self->{_actions} = [];
}


sub _init{
    my $self = shift;
    my $original_id = shift;

    my $db = new Solstice::Database();
    my $config = Solstice::Configure->new();
    my $db_name = $config->getDBName();

    $db->readQuery("SELECT role_id, name, person_id FROM ".$db_name.".Role WHERE role_id = ?",$original_id);

    my $data_ref = $db->fetchRow();
    return $self unless $data_ref;
    $self->_initFromHashRef($data_ref);
    return $self;
}

sub _initFromHashRef {
    my $self = shift;
    my $data_ref = shift;

    $self->setID($data_ref->{'role_id'});
    $self->setName($data_ref->{'name'});
    $self->setSTID($data_ref->{'person_id'});

    my $actions = Solstice::AuthZ::Factory->new()->createActionsByRole($self);

    $self->addActions(@$actions);

    return $self;
}

sub store {
    my $self = shift;

    my $db = new Solstice::Database();
    my $config = Solstice::Configure->new();
    my $db_name = $config->getDBName();

    my $id            = $self->getID();
    my $name        = $self->getName();
    my $stid        = $self->getSTID();


    my $testid;
    if (defined $id) {
        $db->readQuery("SELECT role_id FROM ".$db_name.".Role WHERE role_id = ?",$id);
        $testid = $db->fetchRow()->{'role_id'};
    }

    if($testid){

        $db->writeQuery(
            "UPDATE ".$db_name.".Role SET
                        name = ?,
                        person_id = ?
                        WHERE
                        role_id = ?",$name,$stid,$id
        );

        $db->writeQuery("DELETE FROM ".$db_name.".RolePermissions WHERE role_id = ?", $id);

    }else{

        $db->writeQuery("INSERT INTO ".$db_name.".Role
                        (name, person_id)
                        values
                        (?, ?)",$name,$stid);
        my $id = $db->getLastInsertID();

        $self->setID($id);
    }

    $db->writeLock($db_name.".RolePermissions");

    $id = $self->getID(); #incase of a new insert

    for my $action ($self->getActions()){
        $db->writeQuery("INSERT INTO ".$db_name.".RolePermissions (role_id, action_id) values (?,?)",$id,$action->getID());
    }

    $db->unlockTables();
}


sub delete{
    my $self = shift;

    my $db = new Solstice::Database();
    my $config = Solstice::Configure->new();
    my $db_name = $config->getDBName();

    my $id = $self->getID();

    $db->writeQuery("DELETE FROM ".$db_name.".Role WHERE role_id = ?",$id);
}


1;
__END__