Import users during migration
This commit is contained in:
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,40 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using MileageTraker.Web.DAL;
|
||||||
|
using NUnit.Framework;
|
||||||
|
|
||||||
|
namespace Web.Tests.DAL
|
||||||
|
{
|
||||||
|
[TestFixture]
|
||||||
|
public class UserImporterTests
|
||||||
|
{
|
||||||
|
[Test]
|
||||||
|
public void Import_Gets_More_Than_One_Result()
|
||||||
|
{
|
||||||
|
var users = UserImporter.Import("DAL\\UserEmails.xls");
|
||||||
|
|
||||||
|
Assert.That(users.Count(), Is.GreaterThan(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Import_Disabled_Users()
|
||||||
|
{
|
||||||
|
var users = UserImporter.Import("DAL\\UserEmails.xls");
|
||||||
|
var jlawson = users.Find(u => u.Username == "jlawson");
|
||||||
|
Assert.That(jlawson.IsApproved, Is.False);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Import_Gets_All_Fields()
|
||||||
|
{
|
||||||
|
var users = UserImporter.Import("DAL\\UserEmails.xls");
|
||||||
|
var dsloan = users.Find(u => u.Username == "dsloan");
|
||||||
|
Assert.That(dsloan.Username, Is.EqualTo("dsloan"));
|
||||||
|
Assert.That(dsloan.FullName, Is.EqualTo("David Sloan"));
|
||||||
|
Assert.That(dsloan.Email, Is.EqualTo("dsloan@ethra.org"));
|
||||||
|
Assert.That(dsloan.IsApproved, Is.True);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -83,6 +83,7 @@
|
|||||||
</CodeAnalysisDependentAssemblyPaths>
|
</CodeAnalysisDependentAssemblyPaths>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<Compile Include="DAL\UserImporterTests.cs" />
|
||||||
<Compile Include="DAL\VehicleImporterTests.cs" />
|
<Compile Include="DAL\VehicleImporterTests.cs" />
|
||||||
<Compile Include="Utility\CryptoTests.cs" />
|
<Compile Include="Utility\CryptoTests.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
@@ -105,6 +106,9 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Include="app.config" />
|
<None Include="app.config" />
|
||||||
|
<Content Include="DAL\UserEmails.xls">
|
||||||
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
|
</Content>
|
||||||
<None Include="packages.config" />
|
<None Include="packages.config" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup />
|
<ItemGroup />
|
||||||
|
|||||||
Binary file not shown.
@@ -0,0 +1,43 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Web;
|
||||||
|
using ExcelLibrary.SpreadSheet;
|
||||||
|
using MileageTraker.Web.Models;
|
||||||
|
|
||||||
|
namespace MileageTraker.Web.DAL
|
||||||
|
{
|
||||||
|
public static class UserImporter
|
||||||
|
{
|
||||||
|
public static List<User> Import(string filename)
|
||||||
|
{
|
||||||
|
var fileInfo = new FileInfo(filename);
|
||||||
|
return Import(Workbook.Load(fileInfo.FullName)).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static IEnumerable<User> Import(Workbook workbook)
|
||||||
|
{
|
||||||
|
var sheet = workbook.Worksheets[0];
|
||||||
|
var first = true;
|
||||||
|
for (var r = sheet.Cells.FirstRowIndex; r <= sheet.Cells.LastRowIndex; r++)
|
||||||
|
{
|
||||||
|
var row = sheet.Cells.GetRow(r);
|
||||||
|
|
||||||
|
if (first)
|
||||||
|
{
|
||||||
|
first = false;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
var user = new User();
|
||||||
|
user.FullName = row.GetCell(0).StringValue.Trim();
|
||||||
|
user.Username = row.GetCell(1).StringValue.Trim();
|
||||||
|
user.Email = row.GetCell(2).StringValue.Trim();
|
||||||
|
user.IsApproved = string.IsNullOrEmpty(row.GetCell(3).StringValue);
|
||||||
|
|
||||||
|
yield return user;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,11 +1,88 @@
|
|||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Web;
|
||||||
|
using MileageTraker.Web.DAL;
|
||||||
|
using MileageTraker.Web.Models;
|
||||||
|
|
||||||
namespace MileageTraker.Web.Migrations
|
namespace MileageTraker.Web.Migrations
|
||||||
{
|
{
|
||||||
using System.Data.Entity.Migrations;
|
using System.Data.Entity.Migrations;
|
||||||
|
|
||||||
public partial class AddLogUser : DbMigration
|
public partial class AddLogUser : DbMigration
|
||||||
{
|
{
|
||||||
public override void Up()
|
/// <summary>
|
||||||
|
/// 0 - UserId
|
||||||
|
/// 1 - Username
|
||||||
|
/// 2 - Email
|
||||||
|
/// 3 - FullName
|
||||||
|
/// 4 - IsApproved
|
||||||
|
/// </summary>
|
||||||
|
private const string InsertUserTemplate = @"INSERT INTO [User]
|
||||||
|
([UserId]
|
||||||
|
,[Username]
|
||||||
|
,[Email]
|
||||||
|
,[FullName]
|
||||||
|
,[Password]
|
||||||
|
,[Comment]
|
||||||
|
,[IsApproved]
|
||||||
|
,[PasswordFailuresSinceLastSuccess]
|
||||||
|
,[LastPasswordFailureDate]
|
||||||
|
,[LastActivityDate]
|
||||||
|
,[LastLockoutDate]
|
||||||
|
,[LastLoginDate]
|
||||||
|
,[Created]
|
||||||
|
,[IsLockedOut]
|
||||||
|
,[LastPasswordChangedDate]
|
||||||
|
,[PasswordResetToken])
|
||||||
|
VALUES
|
||||||
|
('{0}'
|
||||||
|
,'{1}'
|
||||||
|
,'{2}'
|
||||||
|
,'{3}'
|
||||||
|
,''
|
||||||
|
,NULL
|
||||||
|
,{4}
|
||||||
|
,0
|
||||||
|
,'1753-1-1'
|
||||||
|
,'1753-1-1'
|
||||||
|
,'1753-1-1'
|
||||||
|
,'1753-1-1'
|
||||||
|
,GETDATE()
|
||||||
|
,0
|
||||||
|
,'1753-1-1'
|
||||||
|
,NULL)";
|
||||||
|
|
||||||
|
private const string InsertUserRoleDriverTemplate =
|
||||||
|
@"INSERT INTO [RoleUser]
|
||||||
|
([Role_RoleId]
|
||||||
|
,[User_UserId])
|
||||||
|
VALUES
|
||||||
|
('C070ADF5-CCA3-42B0-92E4-9E38472940DF'
|
||||||
|
,'{0}')";
|
||||||
|
|
||||||
|
private static string GetUserInsertSql(User user)
|
||||||
|
{
|
||||||
|
return string.Format(InsertUserTemplate,
|
||||||
|
user.UserId,
|
||||||
|
user.Username,
|
||||||
|
user.Email,
|
||||||
|
user.FullName,
|
||||||
|
user.IsApproved ? 1 : 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string GetUserInsertRoleDriverSql(User user)
|
||||||
{
|
{
|
||||||
|
return string.Format(InsertUserRoleDriverTemplate,
|
||||||
|
user.UserId);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Up()
|
||||||
|
{
|
||||||
|
AddColumn("Log", "User_UserId", c => c.Guid());
|
||||||
|
AddForeignKey("Log", "User_UserId", "User", "UserId");
|
||||||
|
CreateIndex("Log", "User_UserId");
|
||||||
|
|
||||||
Sql(@"INSERT INTO [Role]
|
Sql(@"INSERT INTO [Role]
|
||||||
([RoleId]
|
([RoleId]
|
||||||
,[RoleName]
|
,[RoleName]
|
||||||
@@ -15,19 +92,35 @@ namespace MileageTraker.Web.Migrations
|
|||||||
,'Driver'
|
,'Driver'
|
||||||
,'Driver')");
|
,'Driver')");
|
||||||
|
|
||||||
|
//var folder = AppDomain.CurrentDomain.GetData("App_Data").ToString();
|
||||||
AddColumn("Log", "User_UserId", c => c.Guid());
|
var appDomain = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "App_Data");
|
||||||
AddForeignKey("Log", "User_UserId", "User", "UserId");
|
var users = UserImporter.Import(Path.Combine(appDomain, "UserEmails.xls"));
|
||||||
CreateIndex("Log", "User_UserId");
|
users = (from u in users
|
||||||
|
where u.Username != "dsloan" &&
|
||||||
|
u.Username != "ccecil"
|
||||||
|
select u).ToList();
|
||||||
|
foreach (var user in users)
|
||||||
|
{
|
||||||
|
user.UserId = Guid.NewGuid();
|
||||||
|
Sql(GetUserInsertSql(user));
|
||||||
|
Sql(GetUserInsertRoleDriverSql(user));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Down()
|
public override void Down()
|
||||||
{
|
{
|
||||||
DropIndex("Log", new[] { "User_UserId" });
|
DropIndex("Log", new[] { "User_UserId" });
|
||||||
DropForeignKey("Log", "User_UserId", "User");
|
DropForeignKey("Log", "User_UserId", "User");
|
||||||
DropColumn("Log", "User_UserId");
|
DropColumn("Log", "User_UserId");
|
||||||
|
|
||||||
|
Sql(@"DELETE FROM [User]
|
||||||
|
WHERE UserId IN (
|
||||||
|
SELECT RoleUser.User_UserId
|
||||||
|
FROM RoleUser
|
||||||
|
INNER JOIN Role On RoleUser.Role_RoleId = Role.RoleId
|
||||||
|
WHERE RoleName = 'Driver'
|
||||||
|
)");
|
||||||
|
|
||||||
Sql(@"DELETE FROM ROLE WHERE RoleName = 'Driver'");
|
Sql(@"DELETE FROM ROLE WHERE RoleName = 'Driver'");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -125,6 +125,7 @@
|
|||||||
<Compile Include="DAL\CodeFirstMembershipProvider.cs" />
|
<Compile Include="DAL\CodeFirstMembershipProvider.cs" />
|
||||||
<Compile Include="DAL\CodeFirstRoleProvider.cs" />
|
<Compile Include="DAL\CodeFirstRoleProvider.cs" />
|
||||||
<Compile Include="DAL\UserAccountDisabledException.cs" />
|
<Compile Include="DAL\UserAccountDisabledException.cs" />
|
||||||
|
<Compile Include="DAL\UserImporter.cs" />
|
||||||
<Compile Include="DAL\UserLockedOutException.cs" />
|
<Compile Include="DAL\UserLockedOutException.cs" />
|
||||||
<Compile Include="Email\EmailNotification.cs" />
|
<Compile Include="Email\EmailNotification.cs" />
|
||||||
<Compile Include="Migrations\201204181847082_InitialMigration.cs" />
|
<Compile Include="Migrations\201204181847082_InitialMigration.cs" />
|
||||||
@@ -230,6 +231,9 @@
|
|||||||
<Content Include="favicon.ico" />
|
<Content Include="favicon.ico" />
|
||||||
<Content Include="Global.asax" />
|
<Content Include="Global.asax" />
|
||||||
<Content Include="README.jQuery.vsdoc.txt" />
|
<Content Include="README.jQuery.vsdoc.txt" />
|
||||||
|
<Content Include="App_Data\UserEmails.xls">
|
||||||
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
|
</Content>
|
||||||
<None Include="Scripts\jquery-1.8.3.intellisense.js" />
|
<None Include="Scripts\jquery-1.8.3.intellisense.js" />
|
||||||
<Content Include="Scripts\bootstrap.js" />
|
<Content Include="Scripts\bootstrap.js" />
|
||||||
<Content Include="Scripts\bootstrap.min.js" />
|
<Content Include="Scripts\bootstrap.min.js" />
|
||||||
|
|||||||
Reference in New Issue
Block a user