/**
 * File:	installation/general/inst_auth.ycp
 * Package:	Users configuration
 * Summary:	Ask for user authentication method
 * Authors:	Arvin Schnell <arvin@suse.de>
 *		Michal Svec <msvec@suse.cz>
 *
 * $Id: inst_auth.ycp 20879 2005-01-24 16:55:25Z nashif $
 */

{

textdomain "users";

import "DSL";
import "Ldap";
import "NetworkDevices";
import "Package";
import "Popup";
import "Users";
import "Wizard";
import "GetInstArgs";

// Check if lan is configured
list<string> net_devices = NetworkDevices::List("");
y2debug("net_devices: %1", net_devices);

if (size(net_devices) == 0 && size (Ldap::initial_defaults) == 0) {
    Users::SetAfterAuth ("users");
    y2milestone ("network not available: no network based authentization");
    return `auto;
}
boolean found = false;
maplist(string dev, net_devices, ``{
    if(!DSL::UsesDevice(dev))
	found = true;
});
y2debug("found=%1", found);

if (!found && size (Ldap::initial_defaults) == 0) {
    y2milestone ("network not available: no network based authentization");
    Users::SetAfterAuth ("users");
    return `auto;
}

// caption for dialog "User Authentication Method"
string caption = _("User Authentication Method");

// help text for dialog "User Authentication Method" 1/3
string help = _("<p>
<b>Authentication</b><br>
Here you can choose the authentication method of users on your system.
</p>") +

// helptext 2/3
_("<p>Select <b>Local</b> to authenticate users only by using the local files <i>/etc/passwd</i> and <i>/etc/shadow</i>.</p>");

map button_labels = $[
    // radiobutton to select ldap user auth.
    "ldap":      _("&LDAP"),
    // radiobutton to select nis user auth.
    "nis":       _("N&IS"),
    // radiobutton to select samba user auth.
    "samba":       _("&Samba"),
    // radiobutton to select local user auth.
    "users":     _("L&ocal (/etc/passwd)")
];

list<string> available_clients = [ "users", "ldap" ];

if (Package::Available ("yast2-nis-client") && found)
    available_clients = add (available_clients, "nis");

if (Package::Available ("yast2-samba-client") && found)
    available_clients = add (available_clients, "samba");

if (contains (available_clients, "nis"))
{
    if (contains (available_clients, "samba"))
    {
	help = help +
	// helptext 3/3 -- nis & samba & ldap avialable
_("<p>If you are using a NIS or LDAP server to store user data or if you want to authenticate users against an NT server, choose the appropriate value. Then continue with configuration of your client after pressing <b>Next</b>.</p>");
    }
    else
    {
	help = help +
	// helptext 3/3 -- nis & ldap avialable
_("<p>If you are using a NIS or LDAP server to store user data, choose the appropriate value. Then continue with configuration of your client after pressing <b>Next</b>.</p>");
    }
}
else
{
    if (contains (available_clients, "samba"))
    {
	help = help +
	// helptext 3/3 -- samba &ldap available
_("<p>If you are using a LDAP server to store user data or if you want to authenticate users against an NT server, choose the appropriate value. Then continue with configuration of your client after pressing <b>Next</b>.</p>");
    }
    else
    {
	help = help +
	// helptext 3/3 -- only ldap available
_("<p>If you are using an LDAP server to store user data, choose the appropriate value. Then continue with configuration of your client after pressing <b>Next</b>.</p>");
    }
}

term buttons = `VBox (`VSpacing(0.5));

foreach (string client, available_clients, ``{
    buttons = add (buttons,
	`Left(`RadioButton(`id(client), button_labels[client]:"")));
});

buttons = add (buttons, `VSpacing(0.5));

// set LDAP for default, if ldap-server was configured:
if (size (Ldap::initial_defaults) > 0)
    Users::SetAfterAuth ("ldap");

term contents = `VBox (
		       `VStretch(),
		       `HBox (
			      `HStretch(),
			      // frame title for authentication methods
			      `Frame (_("Authentication Method"),
				      `RadioButtonGroup( buttons )
				      ),
			      `HStretch()
			      ),
		       `VStretch()
		       );

Wizard::SetDesktopIcon("users");
Wizard::SetContents (caption, contents, help,
    GetInstArgs::enable_back(), GetInstArgs::enable_next());

// select and enable to correct buttons
foreach (string client, available_clients, ``{
    UI::ChangeWidget (`id(client), `Value, Users::AfterAuth () == client);
});

any ret = nil;

repeat {

    ret = Wizard::UserInput();

    if (ret == `abort)
    {
	if ( Popup::ConfirmAbort (`incomplete) )
	    break;
    }
    else if ( ret == `help )
    {
	Wizard::ShowHelp (help);
    }

} until ( ret == `next || ret == `back );

if ( (boolean) UI::QueryWidget( `id("ldap"), `Value ) )
    Users::SetAfterAuth ("ldap");
if ( (boolean) UI::QueryWidget( `id("users"), `Value ) )
    Users::SetAfterAuth ("users");
if (contains (available_clients, "nis") &&
    (boolean) UI::QueryWidget( `id("nis"), `Value ) )
{
    Users::SetAfterAuth ("nis");
}
if (contains (available_clients, "samba") &&
    (boolean) UI::QueryWidget( `id("samba"), `Value ) )
{
    Users::SetAfterAuth ("samba");
}

return (symbol) ret;

/* EOF */
}
