Advertisement
Guest User

Untitled

a guest
Jan 12th, 2018
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.16 KB | None | 0 0
  1. namespace Smth.Data.Migrations
  2. {
  3.     using Common;
  4.     using Entities;
  5.     using Managers;
  6.     using Microsoft.AspNet.Identity;
  7.     using Microsoft.AspNet.Identity.EntityFramework;
  8.     using System;
  9.     using System.Collections.Generic;
  10.     using System.Data.Entity.Migrations;
  11.     using System.Linq;
  12.  
  13.     internal sealed class Configuration : DbMigrationsConfiguration<ApplicationDbContext>
  14.     {
  15.         public Configuration()
  16.         {
  17.             AutomaticMigrationsEnabled = false;
  18.             ContextKey = "E_TRADING.Data.ApplicationDbContext";
  19.         }
  20.  
  21.         protected override void Seed(ApplicationDbContext context)
  22.         {
  23.             var userManager = new ApplicationUserManager(new UserStore<User>(context));
  24.             var roleManager = new ApplicationRoleManager(new RoleStore<IdentityRole>(context));
  25.  
  26.             var roles = new List<IdentityRole>
  27.             {
  28.                 new IdentityRole
  29.                 {
  30.                     Name = UserRole.SuperAdmin
  31.                 },
  32.                 new IdentityRole
  33.                 {
  34.                     Name = UserRole.Administrator
  35.                 },
  36.                 new IdentityRole
  37.                 {
  38.                     Name = UserRole.Buyer
  39.                 },
  40.                 new IdentityRole
  41.                 {
  42.                     Name = UserRole.Seller
  43.                 },
  44.                 new IdentityRole
  45.                 {
  46.                     Name = UserRole.Banned
  47.                 }
  48.             };
  49.  
  50.             foreach (var identityRole in roles)
  51.             {
  52.                 var existingRole = roleManager.FindByName(identityRole.Name);
  53.                 if (existingRole == null)
  54.                 {
  55.                     roleManager.Create(identityRole);
  56.                 }
  57.             }
  58.  
  59.             var superAdmin = new User //only one, always
  60.             {
  61.                 UserName = "superadmin",
  62.                 Email = "superadmin@admin.com",
  63.                 EmailConfirmed = true,
  64.                 Id = Guid.NewGuid().ToString()
  65.             };
  66.             var superAdminPassword = "q1w2e3r4";
  67.  
  68.             var adminUsers = new List<User>
  69.             {
  70.                 new User
  71.                 {
  72.                     UserName = "admin",
  73.                     Email = "admin@admin.com",
  74.                     EmailConfirmed = true,
  75.                     Id = Guid.NewGuid().ToString()
  76.                 }
  77.             };
  78.  
  79.             var buyerUsers = new List<User>
  80.             {
  81.                 new User
  82.                 {
  83.                     UserName = "buyer",
  84.                     Email = "buyer@buyer.com",
  85.                     EmailConfirmed = true,
  86.                     Id = Guid.NewGuid().ToString()
  87.                 }
  88.             };
  89.  
  90.             var sellerUsers = new List<User>
  91.             {
  92.                 new User
  93.                 {
  94.                     UserName = "seller",
  95.                     Email = "seller@seller.com",
  96.                     EmailConfirmed = true,
  97.                     Id = Guid.NewGuid().ToString()
  98.                 }
  99.             };
  100.  
  101.             var eUser = userManager.FindByName(superAdmin.UserName);
  102.             if (eUser == null)
  103.             {
  104.                 userManager.Create(superAdmin, superAdminPassword);
  105.                 userManager.SetLockoutEnabled(superAdmin.Id, false);
  106.  
  107.                 var rolesForUser = userManager.GetRoles(superAdmin.Id);
  108.                 if (!rolesForUser.Contains(UserRole.SuperAdmin))
  109.                 {
  110.                     userManager.AddToRole(superAdmin.Id, UserRole.SuperAdmin);
  111.                 }
  112.             }
  113.  
  114.             foreach (var applicationUser in adminUsers)
  115.             {
  116.                 var existingUser = userManager.FindByName(applicationUser.UserName);
  117.                 if (existingUser == null)
  118.                 {
  119.                     userManager.Create(applicationUser, Common.Constants.Password);
  120.                     userManager.SetLockoutEnabled(applicationUser.Id, false);
  121.  
  122.                     var rolesForUser = userManager.GetRoles(applicationUser.Id);
  123.                     if (!rolesForUser.Contains(UserRole.Administrator))
  124.                     {
  125.                         userManager.AddToRole(applicationUser.Id, UserRole.Administrator);
  126.                     }
  127.                 }
  128.             }
  129.  
  130.             foreach (var applicationUser in buyerUsers)
  131.             {
  132.                 var existingUser = userManager.FindByName(applicationUser.UserName);
  133.                 if (existingUser == null)
  134.                 {
  135.                     userManager.Create(applicationUser, Common.Constants.Password);
  136.                     userManager.SetLockoutEnabled(applicationUser.Id, false);
  137.  
  138.                     var rolesForUser = userManager.GetRoles(applicationUser.Id);
  139.                     if (!rolesForUser.Contains(UserRole.Buyer))
  140.                     {
  141.                         userManager.AddToRole(applicationUser.Id, UserRole.Buyer);
  142.                     }
  143.  
  144.                     context.Buyers.Add(new Buyer
  145.                     {
  146.                         Id = applicationUser.Id
  147.                     });
  148.                 }
  149.             }
  150.  
  151.             foreach (var applicationUser in sellerUsers)
  152.             {
  153.                 var existingUser = userManager.FindByName(applicationUser.UserName);
  154.                 if (existingUser == null)
  155.                 {
  156.                     userManager.Create(applicationUser, Common.Constants.Password);
  157.                     userManager.SetLockoutEnabled(applicationUser.Id, false);
  158.  
  159.                     var rolesForUser = userManager.GetRoles(applicationUser.Id);
  160.                     if (!rolesForUser.Contains(UserRole.Seller))
  161.                     {
  162.                         userManager.AddToRole(applicationUser.Id, UserRole.Seller);
  163.                     }
  164.  
  165.                     context.Sellers.Add(new Seller
  166.                     {
  167.                         Id = applicationUser.Id,
  168.                         Alias = applicationUser.UserName + "Alias",
  169.                         ContactPhone = "0987654321",
  170.                         OfficeAddress = "Kiev, Sechenova, 6"
  171.                     });
  172.                 }
  173.             }
  174.  
  175.             base.Seed(context);
  176.         }
  177.     }
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement