Krang::User - a means to access information on users
use Krang::ClassLoader 'User';
# construct object
my $user = Krang::User->new(email => 'a@b.com', #optional
first_name => 'fname', #optional
group_ids => [1, 2, 3], #optional
last_name => 'lname', #optional
login => 'login', #required
mobile_phone => '112-358-1321' #optional
password => 'passwd', #required
phone => '123-456-8901'); #optional
# saves object to the DB $user->save();
# getters ########## my $email = $user->email(); my $first_name= $user->first_name(); my @group_ids = $user->group_ids(); # returns arrayref or array my $last_name = $user->last_name(); my $login = $user->login(); my $password = $user->password(); my $phone = $user->phone();
my $id = $user->user_id(); # undef until save()
# setters
##########
$user->first_name( 'first_name' );
$user->group_ids( @ids );
$user->last_name('last_name');
$user->login( 'loginX' );
$user->mobile_phone( $phone_number );
$user->password( $password ); # stores MD5 of $self->SALT, $password
$user->phone( $phone_number );
# delete the user from the database $user->delete();
# a hash of search parameters
my %params =
( order_desc => 1, # result ascend unless this flag is set
limit => 5, # return 5 or less user objects
offset => 1, # start counting result from the
# second row
order_by => 'user_id' # sort on the 'user_id' field
login_like => '%fred%', # match rows with 'login's LIKE '%fred'
phone_like => '718%' ); # match rows with phone#'s LIKE '718%'
# any valid object field can be appended with '_like' to perform a # case-insensitive sub-string match on that field in the database
# returns an array of user objects matching criteria in %params
my @users = pkg('User')->find( %params );
Each user object corresponds to an authorized user of the system. The degree of access a user is determined by the groups with which he is associated.
N.B. - Passwords are MD5 digests of $self->SALT and the password string; the original password string in not retrievable once it is passed but can only be calculated and compared i.e.:
my $valid_password =
$user->{password} eq md5_hex($self->SALT, $password_string) ? 1 : 0;
Access to fields for this object is provided my Krang::MethodMaker. The value of fields can be obtained and set in the following fashion:
$value = $user->field_name(); $user->field_name( $some_value );
The available fields for a user object are:
delete()N.B. - this call may result in an exception as it precipitates a call to dependent check. See $user->dependent_check().
dependent_check()
eval {$user->dependent_check()};
if ($@ && $@->isa('Krang::User::Dependency')) {
my %dependencies = $@->dependencies;
croak("The following objects depend on this user:\n\t" .
join("\n\t", map {"$_: " . join(",", @{$dependencies{$_}})}
keys %dependencies));
}
dependent_check() and mainly
exists to be overridden by addons.
Classes in this list must implement the find() and id_meth() methods.
duplicate_check()Krang::User::Duplicate exception have a 'duplicates' field that contains info about the object that would be duplicated. One can handle the exception thusly:
eval {$user->duplicate_check()};
if ($@ && $@->isa('Krang::User::Duplicate')) {
my %duplicates = $@->duplicates;
croak("The following objects are duplicated on this object:\n\t" .
join("\n\t", map {"$_: " . join(",", @{$duplicates{$_}})}
keys %duplicates));
}
Additional criteria which affect the search results are:
The method croaks if an invalid search criteria is provided or if both the 'count' and 'ids_only' options are specified.
password()save()The method croaks if the save would result in a duplicate object (i.e. if any field value required to be unique is not found to be so). It also croaks if its database query affects no rows in the database.
$user = Krang::User->deserialize_xml(xml => $xml, set => $set, no_update => 0)If an incoming user has the same login as an existing user then an update will occur, unless no_update is set.
$user->may_delete_user($other_user_obj)$user->may_delete_user($other_user_id)$current_user = pkg('User')->current_user_group_ids$current_user = $other_user->current_user_group_ids
Krang, Krang::DB