From bf8dbeee04ef638585dfe66ac92b93da7d498b4e Mon Sep 17 00:00:00 2001 From: Paul Schneider Date: Mon, 26 Jan 2015 00:29:19 +0100 Subject: [PATCH] * DataManager.cs: InitDb on the go * InitDb.aspx: * StaticPage.master: * Created.aspx: * LocalizedText.fr.resx: * AdminController.cs: * LocalizedText.Designer.cs: * Web.csproj: Db initialisation web pages * DataAccess.cs: a connection string * LocalizedText.resx: internationalisaX creating the admin role --- web/Admin/DataManager.cs | 30 +++++++++++++- web/Controllers/AdminController.cs | 60 ++++++++++++++++++++++------ web/Models/StaticPage.master | 20 ---------- web/Views/Admin/Created.aspx | 4 +- web/Views/Admin/InitDb.aspx | 22 ++-------- web/Web.csproj | 3 ++ yavscModel/Admin/DataAccess.cs | 5 +++ yavscModel/LocalizedText.Designer.cs | 24 +++++++++++ yavscModel/LocalizedText.fr.resx | 5 +++ yavscModel/LocalizedText.resx | 5 ++- 10 files changed, 123 insertions(+), 55 deletions(-) diff --git a/web/Admin/DataManager.cs b/web/Admin/DataManager.cs index 17870d58..5a3160a5 100644 --- a/web/Admin/DataManager.cs +++ b/web/Admin/DataManager.cs @@ -3,6 +3,7 @@ using System.Diagnostics; using System.IO; using Yavsc.Model.Admin; using Npgsql.Web.Blog; +using System.Resources; namespace Yavsc.Admin { @@ -67,7 +68,34 @@ namespace Yavsc.Admin public TaskOutput CreateDb () { - return Restore ("freshinstall", false); + TaskOutput res = new TaskOutput (); + + string sql; + try { + using (Stream sqlStream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("Yavsc.instdbws.sql")) + { + using (StreamReader srdr = new StreamReader (sqlStream)) { + sql = srdr.ReadToEnd (); + using (var cnx = new Npgsql.NpgsqlConnection (da.ConnectionString())) { + using (var cmd = cnx.CreateCommand ()) { + cmd.CommandText = sql; + cnx.Open(); + cmd.ExecuteNonQuery(); + cnx.Close(); + } + } + } + } + } + catch (Exception ex) { + res.ExitCode = 1; + res.Error = + string.Format ("Exception of type {0} occured during the script execution", + ex.GetType ().Name); + res.Message = ex.Message; + } + + return res; } public Export TagBackup (string filename, string [] tags) diff --git a/web/Controllers/AdminController.cs b/web/Controllers/AdminController.cs index 4141c6b3..27fe3af4 100644 --- a/web/Controllers/AdminController.cs +++ b/web/Controllers/AdminController.cs @@ -9,6 +9,7 @@ using Yavsc.Model.RolesAndMembers; using Yavsc.Model.Admin; using Yavsc.Admin; using System.IO; +using Yavsc.Model; namespace Yavsc.Controllers @@ -19,13 +20,27 @@ namespace Yavsc.Controllers /// public class AdminController : Controller { - - [Authorize(Roles="Admin")] public ActionResult Index() { + if (!Roles.RoleExists (adminRoleName)) { + Roles.CreateRole (adminRoleName); + } return View (); } + public ActionResult InitDb(DataAccess datac, string doInit) + { + if (doInit=="on") { + if (ModelState.IsValid) { + // TODO BETTER + datac.BackupPrefix = Server.MapPath (datac.BackupPrefix); + DataManager mgr = new DataManager (datac); + TaskOutput t = mgr.CreateDb (); + return View ("Created", t); + } + } + return View (); + } [Authorize(Roles="Admin")] public ActionResult Backups(DataAccess model) { @@ -134,7 +149,13 @@ namespace Yavsc.Controllers ViewData["usertoremove"] = username; return UserList(); } + + //TODO no more than pageSize results per page + /// + /// User list. + /// + /// The list. [Authorize()] public ActionResult UserList () { @@ -142,55 +163,68 @@ namespace Yavsc.Controllers 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); - ViewData["Message"] = "Rôle créé : "+rolename; + ViewData["Message"] = 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"; - protected override void Initialize (System.Web.Routing.RequestContext requestContext) - { - base.Initialize (requestContext); - if (!Roles.RoleExists (adminRoleName)) { - Roles.CreateRole (adminRoleName); - } - } + + + /// + /// Assing the Admin role to the specified user in model. + /// + /// Model. [Authorize()] public ActionResult Admin (NewAdminModel model) { string currentUser = Membership.GetUser ().UserName; if (ModelState.IsValid) { Roles.AddUserToRole (model.UserName, adminRoleName); - ViewData ["Message"] = model.UserName + " was added to the role '" + adminRoleName + "'"; + ViewData ["Message"] = model.UserName + " "+LocalizedText.was_added_to_the_role+" '" + adminRoleName + "'"; } else { // ASSERT (Roles.RoleExists (adminRoleName)) string [] admins = Roles.GetUsersInRole (adminRoleName); if (admins.Length > 0) { if (! admins.Contains (Membership.GetUser ().UserName)) { ModelState.Remove("UserName"); - ModelState.AddModelError("UserName", "You're not administrator!"); + ModelState.AddModelError("UserName",LocalizedText.younotadmin+"!"); return View ("Index"); } } else { Roles.AddUserToRole (currentUser, adminRoleName); admins = new string[] { currentUser }; ViewData ["Message"] += string.Format ( - "There was no user in the 'Admin' role. You ({0}) was just added as the firt user in the 'Admin' role. ", currentUser); + LocalizedText.was_added_to_the_empty_role, + currentUser, adminRoleName); } List users = new List (); diff --git a/web/Models/StaticPage.master b/web/Models/StaticPage.master index 1ce0c8bf..07c92895 100644 --- a/web/Models/StaticPage.master +++ b/web/Models/StaticPage.master @@ -45,26 +45,6 @@