# Copyright (c) 2005-2010 bivio Software, Inc. All Rights Reserved. # # Visit http://www.bivio.biz for more info. # # This library is free software; you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as # published by the Free Software Foundation; either version 2.1 of the # License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; If not, you may get a copy from: # http://www.opensource.org/licenses/lgpl-license.html # # $Id: RealmUserAddForm.pm,v 1.29 2012/01/17 00:02:28 nagler Exp $ package Bivio::Biz::Model::RealmUserAddForm; use strict; use Bivio::Base 'Model.UserRegisterForm'; #TODO: Problably should not subclass UserRegisterForm, but should call # UserCreateForm to create the user our($VERSION) = sprintf('%d.%02d', q$Revision: 1.29 $ =~ /\d+/g); my($_RU) = b_use('ShellUtil.RealmUser'); my($_R) = b_use('Auth.Role'); sub copy_admins { my($self, $realm_id, $admin_user_id) = @_; my($req) = $self->get_request; foreach my $admin_id ( ref($admin_user_id) ? @$admin_user_id : $admin_user_id ? $admin_user_id : _admin_list($self), ) { $self->new->process({ 'RealmUser.realm_id' => $realm_id, 'User.user_id' => $admin_id, administrator => 1, }); } return; } sub execute_ok { my($self) = @_; _join_user( $self, $self->internal_user_id || return, $self->internal_realm_id, ); return; } sub internal_get_roles { my($self) = @_; return [ map($_R->from_name($_), $self->unsafe_get('not_mail_recipient') ? () : 'MAIL_RECIPIENT', $self->unsafe_get('administrator') ? qw(ADMINISTRATOR FILE_WRITER) : ( 'MEMBER', $self->unsafe_get('file_writer') ? 'FILE_WRITER' : (), ), ), ]; } sub internal_initialize { my($self) = @_; return $self->merge_initialize_info($self->SUPER::internal_initialize, { version => 1, require_context => 1, $self->field_decl(visible => [qw(not_mail_recipient administrator file_writer)], qw(Boolean NONE), ), other => [ { # Match RealmUserDeleteForm name => 'realm', type => 'RealmOwner.name', constraint => 'NONE', }, { # Match RealmUserDeleteForm name => 'other_roles', type => 'Array', constraint => 'NONE', }, 'RealmUser.realm_id', ], }); } sub internal_realm_id { my($self) = @_; my($id) = $self->unsafe_get('RealmUser.realm_id'); $self->internal_put_field('RealmUser.realm_id' => $id = $self->unsafe_get('realm') && $self->new_other('RealmOwner') ->unauth_load_or_die({name => $self->get('realm')}) ->get('realm_id') || $self->get_request->get('auth_id'), ) unless $id; return $id; } sub internal_user_id { my($self) = @_; my($id) = $self->unsafe_get('User.user_id'); my($e) = $self->new_other('Email'); $self->internal_put_field('User.user_id' => $id = $e->unauth_load({email => $self->get('Email.email')}) ? $e->get('realm_id') : (($self->internal_create_models)[0] || return)->get('realm_id'), ) unless $id; return $id; } sub _admin_list { my($self) = @_; return @{$self->new_other('RealmAdminList')->map_iterate( sub {shift->get('RealmUser.user_id')}, 'unauth_iterate_start', {auth_id => $self->req('auth_id')}, )}; } sub _join_user { my($self, $user_id, $realm_id) = @_; $self->internal_put_field('RealmUser.realm_id' => $realm_id); $self->internal_put_field('User.user_id' => $user_id); # Just in case there's another RealmUser record my($v) = { user_id => $user_id, realm_id => $realm_id, }; foreach my $r ( @{$self->unsafe_get('other_roles') || []}, @{$self->internal_get_roles}, ) { $self->new_other('RealmUser')->unauth_create_or_update({ %$v, role => $r, }); } $self->req->with_realm_and_user($realm_id, $user_id, sub { $_RU->new->audit_user; }) if $_RU->IS_AUDIT_ENABLED; return; } 1;