package Dezi::Admin::Config;
use strict;
use warnings;
use Carp;
use Dezi::Admin::UI;
use Dezi::Admin::API;
use Class::Inspector;
use Path::Class;
use Plack::Util::Accessor qw(

our $VERSION = '0.006';

=head1 NAME

Dezi::Admin::Config - Dezi administration UI configuration


 use Dezi::Admin::Config;

 my $dac = Dezi::Admin::Config->new(
     user_config => {
         username => 'myuser',
         password => 'secret',
         auth_realm => 'the dezi admin area',
         extjs_url  => '//uri.for/extjs/4.1.x',
         debug      => 1,
     searcher => $dezi_server,
     base_uri => 'http://yourserver/dezi',


Dezi::Admin::Config is used internally by Dezi::Admin to instantiate
the various components (UI, API, etc) that make up the application.

=head1 METHODS


=head2 new( I<args> )

Returns a new Config object. I<args> should be a hash with keys


=item user_config

Hashref as passed to Dezi::Config->new(). May contain a key
called C<admin> with values like:


=item username

=item password

=item auth_realm

=item debug

=item extjs_uri


=item searcher

A Dezi::Server-like instance.

=item base_uri

The base URI for the Dezi instance.



sub new {
    my $class       = shift;
    my %args        = @_;
    my $user_config = delete $args{user_config}
        or croak "user_config required";
    my $searcher = delete $args{searcher}
        or croak "searcher required";
    my $base_uri = delete $args{base_uri} || '';

    my $admin_conf = $user_config->{admin} ||= {};
    $admin_conf->{debug} = 0 unless defined $admin_conf->{debug};
        or carp "WARNING: username missing - no auth enforced";
        or carp "WARNING: password missing - no auth enforced";
    $admin_conf->{auth_realm} ||= 'Dezi Admin';

    if ( $admin_conf->{username} and $admin_conf->{password} ) {
        $admin_conf->{authenticator} = sub {
            my ( $u, $p ) = @_;

       #return 1 if ( !$admin_conf->{username} and !$admin_conf->{password} );
            return $u eq $admin_conf->{username}
                && $p eq $admin_conf->{password};
    else {
        $admin_conf->{authenticator} = undef;

    $admin_conf->{ui_server} = Dezi::Admin::UI->new(
        debug     => $admin_conf->{debug},
        base_uri  => $base_uri,
        extjs_uri => $admin_conf->{extjs_uri},

    my $self = bless $admin_conf, $class;

    $self->{api_server} = Dezi::Admin::API->app(
        admin_config => $self,
        searcher     => $searcher,

    return $self;

=head2 authenticator

The authenticator() method will return a CODE ref for passing
to L<Plack::Middleware::Auth::Basic>.


=head2 ui_server

Returns an instance of Dezi::Admin::UI.

=head2 ui_static_path

File path to where static assets are stored. These include .css
and .js files.


sub ui_static_path {
    my $self = shift;
    my $base = Class::Inspector->loaded_filename('Dezi::Admin::UI');
    $base =~ s/\.pm$//;
    return dir( $base, 'static' );

=head2 api_server

Returns an instance of Dezi::Admin::API.

=head2 as_hash

Returns the object as a plain Perl hash of key/value pairs.


sub as_hash {
    my $self = shift;
    return %$self;



=head1 AUTHOR

Peter Karman, C<< <karman at> >>

=head1 BUGS

Please report any bugs or feature requests to C<bug-dezi-admin at>, or through
the web interface at L<>.  I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.

=head1 SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc Dezi::Admin

You can also look for information at:

=over 4

=item * RT: CPAN's request tracker


=item * AnnoCPAN: Annotated CPAN documentation


=item * CPAN Ratings


=item * Search CPAN




Copyright 2013 Peter Karman.

This program is free software; you can redistribute it and/or modify it
under the terms of either: the GNU General Public License as published
by the Free Software Foundation; or the Artistic License.

See for more information.