View | Details | Raw Unified | Return to bug 14539
Collapse All | Expand All

(-)a/Koha/AuthUtils.pm (+39 lines)
Lines 22-27 use Crypt::Eksblowfish::Bcrypt qw(bcrypt en_base64); Link Here
22
use Encode qw( encode is_utf8 );
22
use Encode qw( encode is_utf8 );
23
use Fcntl qw/O_RDONLY/; # O_RDONLY is used in generate_salt
23
use Fcntl qw/O_RDONLY/; # O_RDONLY is used in generate_salt
24
24
25
use Koha::Borrower;
26
25
use base 'Exporter';
27
use base 'Exporter';
26
28
27
our $VERSION = '1.01';
29
our $VERSION = '1.01';
Lines 133-138 sub generate_salt { Link Here
133
    close SOURCE;
135
    close SOURCE;
134
    return $string;
136
    return $string;
135
}
137
}
138
139
=head checkKohaSuperuserFromUserid
140
See checkKohaSuperuser(), with only the "user identifier"-@PARAM.
141
@THROWS nothing.
142
=cut
143
144
sub checkKohaSuperuserFromUserid {
145
    my ($userid) = @_;
146
147
    if ( $userid && $userid eq C4::Context->config('user') ) {
148
        return _createTemporarySuperuser();
149
    }
150
}
151
152
=head _createTemporarySuperuser
153
154
Create a temporary superuser which should be instantiated only to the environment
155
and then discarded. So do not ->store() it!
156
@RETURN Koha::Borrower
157
=cut
158
159
sub _createTemporarySuperuser {
160
    my $borrower = Koha::Borrower->new();
161
162
    my $superuserName = C4::Context->config('user');
163
    $borrower->set({borrowernumber => 0,
164
                       userid     => $superuserName,
165
                       cardnumber => $superuserName,
166
                       firstname  => $superuserName,
167
                       surname    => $superuserName,
168
                       branchcode => 'NO_LIBRARY_SET',
169
                       flags      => 1,
170
                       email      => C4::Context->preference('KohaAdminEmailAddress')
171
                    });
172
    return $borrower;
173
}
174
136
1;
175
1;
137
176
138
__END__
177
__END__
(-)a/Koha/Borrowers.pm (-2 / +69 lines)
Lines 18-28 package Koha::Borrowers; Link Here
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
use Scalar::Util qw(blessed);
21
22
22
use Carp;
23
use Carp;
23
24
24
use Koha::Database;
25
use Koha::Database;
25
26
use Koha::AuthUtils;
26
use Koha::Borrower;
27
use Koha::Borrower;
27
28
28
use base qw(Koha::Objects);
29
use base qw(Koha::Objects);
Lines 49-54 sub object_class { Link Here
49
    return 'Koha::Borrower';
50
    return 'Koha::Borrower';
50
}
51
}
51
52
53
=head castToBorrower
54
55
    my $borrower = Koha::Borrowers::castToBorrower('cardnumber');
56
    my $borrower = Koha::Borrowers::castToBorrower($Koha::Borrower);
57
    my $borrower = Koha::Borrowers::castToBorrower('userid');
58
    my $borrower = Koha::Borrowers::castToBorrower('borrowernumber');
59
    my $borrower = Koha::Borrowers::castToBorrower({borrowernumber => 123,
60
                                                });
61
    my $borrower = Koha::Borrowers::castToBorrower({firstname => 'Olli-Antti',
62
                                                    surname => 'Kivi',
63
                                                    address => 'Koskikatu 25',
64
                                                });
65
66
Because there are gazillion million ways in Koha to invoke a Borrower, this is a
67
omnibus for easily creating a Borrower-object from all the arcane invocations present
68
in many parts of Koha.
69
Just throw the crazy and unpredictable return values from myriad subroutines returning
70
some kind of an borrowerish value to this casting function to get a brand new Koha::Borrower.
71
@PARAM1 Scalar, or HASHRef.
72
@RETURNS Koha::Borrower, possibly already in DB or a completely new one if nothing was
73
                         inferred from the DB.
74
@THROWS Koha::Exception::BadParameter, if no idea what to do with the input.
75
@THROWS Koha::Exception::UnknownObject, if we cannot find a Borrower with the given input.
76
=cut
77
78
sub castToBorrower {
79
    my ($input) = @_;
80
81
    unless ($input) {
82
        Koha::Exception::BadParameter->throw(error => __PACKAGE__."::castToBorrower():> No parameter given!");
83
    }
84
    if (blessed($input) && $input->isa('Koha::Borrower')) {
85
        return $input;
86
    }
87
    if (blessed($input) && $input->isa('Koha::Schema::Result::Borrower')) {
88
        return Koha::Borrower->_new_from_dbic($input);
89
    }
90
91
    my ($borrowernumber, $cardnumber, $userid, $borrower);
92
    #Extract unique keys and try to get the borrower from them.
93
    if (ref($input) eq 'HASH') {
94
        $borrowernumber = $input->{borrowernumber};
95
        $cardnumber = $input->{cardnumber};
96
        $userid = $input->{userid};
97
    }
98
    elsif (not(ref($input))) { #We have a scalar
99
        $borrowernumber = $input;
100
        $cardnumber = $input;
101
        $userid = $input;
102
    }
103
    if ($borrowernumber || $cardnumber || $userid) {
104
        $borrower = Koha::Borrowers->search({'-or' => [{borrowernumber => $borrowernumber},
105
                                                     {cardnumber => $cardnumber},
106
                                                     {userid => $userid},
107
                                                    ]
108
                                        })->next();
109
        unless ($borrower) {
110
            $borrower = Koha::AuthUtils::checkKohaSuperuserFromUserid($userid);
111
            unless ($borrower) {
112
                Koha::Exception::UnknownObject->throw(error => "Koha::Borrower::castToBorrower->new():> Cannot find an existing Borrower from borrowernumber|cardnumber|userid '$borrowernumber|$cardnumber|$userid'.");
113
            }
114
        }
115
    }
116
117
    return $borrower;
118
}
119
52
=head1 AUTHOR
120
=head1 AUTHOR
53
121
54
Kyle M Hall <kyle@bywatersolutions.com>
122
Kyle M Hall <kyle@bywatersolutions.com>
55
- 

Return to bug 14539