using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using System.Web.Security;
using Yavsc.Model.RolesAndMembers;
using Yavsc.Model.Admin;
using Yavsc.Admin;
using System.IO;
using Yavsc.Model;
using Yavsc.Helpers;
namespace Yavsc.Controllers
{
///
/// Admin controller.
/// Only Admin members should be allowed to use it.
///
public class AdminController : Controller
{
///
/// Index this instance.
///
public ActionResult Index()
{
if (!Roles.RoleExists (adminRoleName)) {
Roles.CreateRole (adminRoleName);
YavscHelpers.Notify (ViewData, adminRoleName + " " + LocalizedText.role_created);
}
return View ();
}
///
/// Inits the db.
/// In order this action succeds,
/// there must not exist any administrator,
/// nor Admin group.
///
/// The db.
/// Datac.
/// Do init.
public ActionResult InitDb(DataAccess datac, string doInit)
{
if (doInit=="on") {
if (ModelState.IsValid) {
datac.BackupPrefix = Server.MapPath (datac.BackupPrefix);
DataManager mgr = new DataManager (datac);
TaskOutput tcdb = mgr.CreateDb ();
ViewData ["DbName"] = datac.DbName;
ViewData ["DbUser"] = datac.DbUser;
ViewData ["Host"] = datac.Host;
ViewData ["Port"] = datac.Port;
return View ("Created", tcdb);
}
}
return View ();
}
///
/// Backups the specified model.
///
/// Model.
[Authorize(Roles="Admin")]
public ActionResult Backups(DataAccess model)
{
return View (model);
}
///
/// Creates the backup.
///
/// The backup.
/// Datac.
[Authorize(Roles="Admin")]
public ActionResult CreateBackup(DataAccess datac)
{
if (datac != null) {
if (ModelState.IsValid) {
if (string.IsNullOrEmpty (datac.Password))
ModelState.AddModelError ("Password", "Invalid passord");
datac.BackupPrefix = Server.MapPath (datac.BackupPrefix);
DataManager ex = new DataManager (datac);
Export e = ex.CreateBackup ();
if (e.ExitCode > 0)
ModelState.AddModelError ("Password", "Operation Failed");
return View ("BackupCreated", e);
}
} else {
datac = new DataAccess ();
}
return View (datac);
}
///
/// Creates the user backup.
///
/// The user backup.
/// Datac.
/// Username.
[Authorize(Roles="Admin")]
public ActionResult CreateUserBackup(DataAccess datac,string username)
{
throw new NotImplementedException();
}
///
/// Upgrade the specified datac.
///
/// Datac.
[Authorize(Roles="Admin")]
public ActionResult Upgrade(DataAccess datac) {
throw new NotImplementedException();
}
///
/// Restore the specified datac, backupName and dataOnly.
///
/// Datac.
/// Backup name.
/// If set to true data only.
[Authorize(Roles="Admin")]
public ActionResult Restore(DataAccess datac,string backupName,bool dataOnly=true)
{
ViewData ["BackupName"] = backupName;
if (ModelState.IsValid) {
// TODO BETTER
datac.BackupPrefix = Server.MapPath (datac.BackupPrefix);
DataManager mgr = new DataManager (datac);
ViewData ["BackupName"] = backupName;
ViewData ["DataOnly"] = dataOnly;
TaskOutput t = mgr.Restore (
Path.Combine(new FileInfo(datac.BackupPrefix).DirectoryName,
backupName),dataOnly);
return View ("Restored", t);
}
BuildBackupList (datac);
return View (datac);
}
private void BuildBackupList(DataAccess datac)
{
// build ViewData ["Backups"];
string bckd=Server.MapPath (datac.BackupPrefix);
DirectoryInfo di = new DirectoryInfo (new FileInfo(bckd).DirectoryName);
List bks = new List ();
foreach (FileInfo ti in di.GetFiles("*.tar"))
bks.Add (ti.Name);
ViewData ["Backups"] = bks.ToArray ();
}
///
/// Removes from role.
///
/// The from role.
/// Username.
/// Rolename.
/// Return URL.
[Authorize(Roles="Admin")]
public ActionResult RemoveFromRole(string username, string rolename, string returnUrl)
{
Roles.RemoveUserFromRole(username,rolename);
return Redirect(returnUrl);
}
///
/// Removes the user.
///
/// The user.
/// Username.
/// Submitbutton.
[Authorize(Roles="Admin")]
public ActionResult RemoveUser (string username, string submitbutton)
{
ViewData ["usertoremove"] = username;
if (submitbutton == "Supprimer") {
Membership.DeleteUser (username);
YavscHelpers.Notify(ViewData, string.Format("utilisateur \"{0}\" supprimé",username));
ViewData ["usertoremove"] = null;
}
return View ();
}
///
/// Removes the role.
///
/// The role.
/// Rolename.
/// Submitbutton.
[Authorize(Roles="Admin")]
public ActionResult RemoveRole (string rolename, string submitbutton)
{
if (submitbutton == "Supprimer")
{
Roles.DeleteRole(rolename);
}
return RedirectToAction("RoleList");
}
///
/// Removes the role query.
///
/// The role query.
/// Rolename.
[Authorize(Roles="Admin")]
public ActionResult RemoveRoleQuery(string rolename)
{
ViewData["roletoremove"] = rolename;
return View ();
}
///
/// Removes the user query.
///
/// The user query.
/// Username.
[Authorize(Roles="Admin")]
public ActionResult RemoveUserQuery(string username)
{
ViewData["usertoremove"] = username;
return UserList();
}
//TODO no more than pageSize results per page
///
/// User list.
///
/// The list.
[Authorize()]
public ActionResult UserList ()
{
MembershipUserCollection c = Membership.GetAllUsers ();
return View (c);
}
///
/// a form to add a role
///
/// The role.
[Authorize(Roles="Admin")]
public ActionResult AddRole ()
{
return View ();
}
///
/// Add a new role.
///
/// The add role.
/// Rolename.
[Authorize(Roles="Admin")]
public ActionResult DoAddRole (string rolename)
{
Roles.CreateRole(rolename);
YavscHelpers.Notify(ViewData, LocalizedText.role_created+ " : "+rolename);
return View ();
}
///
/// Shows the roles list.
///
/// The list.
[Authorize()]
public ActionResult RoleList ()
{
return View (Roles.GetAllRoles ());
}
private const string adminRoleName = "Admin";
///
/// Assing the Admin role to the specified user in model.
///
/// Model.
[Authorize()]
public ActionResult Admin (NewAdminModel model)
{
// ASSERT (Roles.RoleExists (adminRoleName))
string [] admins = Roles.GetUsersInRole (adminRoleName);
string currentUser = Membership.GetUser ().UserName;
List users = new List ();
foreach (MembershipUser u in Membership.GetAllUsers ()) {
var i = new SelectListItem ();
i.Text = string.Format ("{0} <{1}>", u.UserName, u.Email);
i.Value = u.UserName;
users.Add (i);
}
ViewData ["admins"] = admins;
ViewData ["useritems"] = users;
if (ModelState.IsValid) {
Roles.AddUserToRole (model.UserName, adminRoleName);
YavscHelpers.Notify(ViewData, model.UserName + " "+LocalizedText.was_added_to_the_role+" '" + adminRoleName + "'");
} else {
if (admins.Length > 0) {
if (! admins.Contains (Membership.GetUser ().UserName)) {
ModelState.Remove("UserName");
ModelState.AddModelError("UserName",LocalizedText.younotadmin+"!");
return View ("Index");
}
} else {
// No admin, gives the Admin Role to the current user
Roles.AddUserToRole (currentUser, adminRoleName);
admins = new string[] { currentUser };
YavscHelpers.Notify(ViewData, string.Format (
LocalizedText.was_added_to_the_empty_role,
currentUser, adminRoleName));
}
}
return View (model);
}
}
}