* 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
This commit is contained in:
@ -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)
|
||||
|
@ -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
|
||||
/// </summary>
|
||||
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
|
||||
/// <summary>
|
||||
/// User list.
|
||||
/// </summary>
|
||||
/// <returns>The list.</returns>
|
||||
[Authorize()]
|
||||
public ActionResult UserList ()
|
||||
{
|
||||
@ -142,55 +163,68 @@ namespace Yavsc.Controllers
|
||||
return View (c);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// a form to add a role
|
||||
/// </summary>
|
||||
/// <returns>The role.</returns>
|
||||
[Authorize(Roles="Admin")]
|
||||
public ActionResult AddRole ()
|
||||
{
|
||||
return View ();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add a new role.
|
||||
/// </summary>
|
||||
/// <returns>The add role.</returns>
|
||||
/// <param name="rolename">Rolename.</param>
|
||||
[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 ();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Shows the roles list.
|
||||
/// </summary>
|
||||
/// <returns>The list.</returns>
|
||||
[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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Assing the Admin role to the specified user in model.
|
||||
/// </summary>
|
||||
/// <param name="model">Model.</param>
|
||||
[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<SelectListItem> users = new List<SelectListItem> ();
|
||||
|
@ -45,26 +45,6 @@
|
||||
<aside>
|
||||
<asp:ContentPlaceHolder ID="MASContent" runat="server">
|
||||
</asp:ContentPlaceHolder>
|
||||
<div id="login">
|
||||
<% if (Membership.GetUser()==null) { %>
|
||||
<%= Html.ActionLink( YavscHelpers.SiteName, "Index", "Home" ,null, new { @class="actionlink" } ) %>
|
||||
<span class="hidcom"> Page d'accueil </span>
|
||||
<%= Html.ActionLink("Login", "Login", "Account", new { returnUrl=Request.Url.PathAndQuery }, new { @class="actionlink" } ) %>
|
||||
<span class="hidcom">Pour pouvoir poster ou commenter</span>
|
||||
<a href="<%=Request.Url.Scheme + "://" + Request.Url.Authority + "/Google/Login"%>?returnUrl=<%=Request.Url.PathAndQuery%>" class="actionlink">
|
||||
<img src="/images/sign-in-with-google-s.png" style="max-height:1.5em; max-width:6em;" alt="Google sign in">
|
||||
</a>
|
||||
<span class="hidcom">S'authentifier avec son compte Google+</span>
|
||||
<% } else { %>
|
||||
<%= Html.ActionLink(HttpContext.Current.User.Identity.Name, "Profile", "Account", null, new { @class="actionlink" }) %>
|
||||
<span class="hidcom"> Édition de votre profile </span>
|
||||
@ <%= Html.ActionLink( YavscHelpers.SiteName, "Index", "Home" ,null, new { @class="actionlink" }) %>
|
||||
<span class="hidcom"> Page d'accueil </span>
|
||||
<a href="/Blogs/Post" class="actionlink">Poster</a>
|
||||
<span class="hidcom"> Édition d'un nouveau billet </span>
|
||||
<%= Html.ActionLink( "Deconnexion", "Logout", "Account", new { returnUrl=Request.Url.PathAndQuery }, new { @class="actionlink" }) %>
|
||||
<% } %>
|
||||
</div>
|
||||
</aside>
|
||||
<footer>
|
||||
<% foreach ( string link in Yavsc.ThanksHelper.Links()) { %>
|
||||
|
@ -1,6 +1,6 @@
|
||||
<%@ Page Language="C#" MasterPageFile="~/Models/App.master" Inherits="System.Web.Mvc.ViewPage<TaskOutput>" %>
|
||||
<%@ Page Title="Db init" Language="C#" MasterPageFile="~/Models/App.master" Inherits="System.Web.Mvc.ViewPage<TaskOutput>" %>
|
||||
<asp:Content ID="MainContentContent" ContentPlaceHolderID="MainContent" runat="server">
|
||||
<h1><%=Html.Encode(ViewData["BackupName"])%> Restauration</h1>
|
||||
<h1>Initialisation de la base de données</h1>
|
||||
<div><h2>Error message </h2> <%= Html.Encode(Model.Error) %></div>
|
||||
<div><h2>Message </h2> <%= Html.Encode(Model.Message) %></div>
|
||||
<div><h2>Exit Code</h2> <%= Html.Encode(Model.ExitCode) %></div>
|
||||
|
@ -1,21 +1,7 @@
|
||||
<%@ Page Title="Restore" Language="C#" MasterPageFile="~/Models/App.master" Inherits="System.Web.Mvc.ViewPage<DataAccess>" %>
|
||||
<%@ Page Title="Init db" Language="C#" MasterPageFile="~/Models/StaticPage.master" Inherits="System.Web.Mvc.ViewPage<DataAccess>" %>
|
||||
<asp:Content ID="MainContentContent" ContentPlaceHolderID="MainContent" runat="server">
|
||||
<%= Html.ValidationSummary("Restore a database backup") %>
|
||||
<% using (Html.BeginForm("Restore","Admin")) { %>
|
||||
|
||||
<% string [] bcfiles = (string[]) ViewData["Backups"]; %>
|
||||
<select name="backupName">
|
||||
<% foreach (string s in bcfiles)
|
||||
{
|
||||
%>
|
||||
<option value="<%=s%>"><%=s%></option>
|
||||
<%
|
||||
}
|
||||
%>
|
||||
</select>
|
||||
<label for="dataOnly">Data only :</label>
|
||||
<%= Html.CheckBox("dataOnly")%>
|
||||
|
||||
<%= Html.ValidationSummary("Init a new data base") %>
|
||||
<% using (Html.BeginForm("InitDb","Admin")) { %>
|
||||
<%= Html.LabelFor(model => model.Host) %>:
|
||||
<%= Html.TextBox( "Host" ) %>
|
||||
<%= Html.ValidationMessage("Host", "*") %><br/>
|
||||
@ -31,7 +17,7 @@
|
||||
<%= Html.LabelFor(model => model.Password) %>:
|
||||
<%= Html.Password( "Password" ) %>
|
||||
<%= Html.ValidationMessage("Password", "*") %><br/>
|
||||
|
||||
<label for="doInit">Executer le script de création de la base:</label><input type="checkbox" name="doInit" id="doInit" >
|
||||
<input type="submit"/>
|
||||
<% } %>
|
||||
</asp:Content>
|
||||
|
@ -632,6 +632,9 @@
|
||||
<Content Include="Views\Google\GoogleErrorMessage.aspx" />
|
||||
<Content Include="Views\Home\Contact.aspx" />
|
||||
<Content Include="Views\Home\AssemblyInfo.aspx" />
|
||||
<Content Include="Views\Admin\Created.aspx" />
|
||||
<Content Include="Views\Admin\InitDb.aspx" />
|
||||
<Content Include="Models\StaticPage.master" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
||||
<Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" />
|
||||
|
@ -69,6 +69,11 @@ namespace Yavsc.Model.Admin
|
||||
get { return dbpassword; }
|
||||
set { dbpassword = value; }
|
||||
}
|
||||
|
||||
public string ConnectionString() {
|
||||
return string.Format ("Server={0};Port={1};Database={2};User Id={3};Password={4};Encoding=Unicode;",
|
||||
Host,Port,Dbuser,Password);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
24
yavscModel/LocalizedText.Designer.cs
generated
24
yavscModel/LocalizedText.Designer.cs
generated
@ -46,6 +46,12 @@ namespace Yavsc.Model {
|
||||
}
|
||||
}
|
||||
|
||||
public static string was_added_to_the_role {
|
||||
get {
|
||||
return ResourceManager.GetString("was_added_to_the_role", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
public static string Unitary_cost {
|
||||
get {
|
||||
return ResourceManager.GetString("Unitary_cost", resourceCulture);
|
||||
@ -136,6 +142,12 @@ namespace Yavsc.Model {
|
||||
}
|
||||
}
|
||||
|
||||
public static string younotadmin {
|
||||
get {
|
||||
return ResourceManager.GetString("younotadmin", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
public static string Description {
|
||||
get {
|
||||
return ResourceManager.GetString("Description", resourceCulture);
|
||||
@ -166,6 +178,12 @@ namespace Yavsc.Model {
|
||||
}
|
||||
}
|
||||
|
||||
public static string was_added_to_the_empty_role {
|
||||
get {
|
||||
return ResourceManager.GetString("was_added_to_the_empty_role", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
public static string MinDate {
|
||||
get {
|
||||
return ResourceManager.GetString("MinDate", resourceCulture);
|
||||
@ -202,6 +220,12 @@ namespace Yavsc.Model {
|
||||
}
|
||||
}
|
||||
|
||||
public static string role_created {
|
||||
get {
|
||||
return ResourceManager.GetString("role_created", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
public static string Register {
|
||||
get {
|
||||
return ResourceManager.GetString("Register", resourceCulture);
|
||||
|
@ -41,4 +41,9 @@
|
||||
<data name="Remember_me"><value>Se souvenir du mot de passe</value></data>
|
||||
<data name="DocTemplateException"><value>Une erreur est survenue à la génération de votre document</value></data>
|
||||
<data name="Message_sent"><value>Votre message a été envoyé</value></data>
|
||||
<data name="was_added_to_the_role"><value>a été ajouté au rôle</value></data>
|
||||
<data name="was_added_to_the_empty_role"><value>Il n'y avait pas 'utilisateur dans le rôle '{1}'. Vous ({0}) avez été ajouté au rôle '{1}'.</value></data>
|
||||
<data name="younotadmin"><value>Vous n'êtes pas administrateur</value></data>
|
||||
<data name="role_created"><value>Rôle créé</value></data>
|
||||
|
||||
</root>
|
||||
|
@ -41,5 +41,8 @@
|
||||
<data name="Remember_me"><value>Remember me</value></data>
|
||||
<data name="DocTemplateException"><value>Exception occured when rendering your document</value></data>
|
||||
<data name="Message_sent"><value>Your message has been sent.</value></data>
|
||||
|
||||
<data name="was_added_to_the_role"><value>was added to the role</value></data>
|
||||
<data name="was_added_to_the_empty_role"><value>There was no user in the '{1}' role. You ({0}) was just added as firt user in the '{1}' role.</value></data>
|
||||
<data name="younotadmin"><value>You're not administrator</value></data>
|
||||
<data name="role_created"><value>role created</value></data>
|
||||
</root>
|
||||
|
Reference in New Issue
Block a user