public async Task <IHttpActionResult> Register(RegisterBindingModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } NotificationsHub.NotifyAdmin("New user is added..."); var appUser = new AppUser() { FullName = model.FullName, Email = model.Email, BirthDay = model.DateOfBirth, /*CreatingServicesBan = true,*/ /*IsRegistered = false*/ }; var user = new RAIdentityUser() { Id = model.Email, UserName = model.Email, Email = model.Email, AppUser = appUser }; user.PasswordHash = RAIdentityUser.HashPassword(model.Password); IdentityResult result = await UserManager.CreateAsync(user, model.Password); await UserManager.AddToRoleAsync(model.Email, "AppUser"); if (!result.Succeeded) { return(Content(System.Net.HttpStatusCode.BadRequest, "User already exists!")); } return(Ok()); }
public async Task <IHttpActionResult> Register(RegisterBindingModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var appUser = new AppUser(); appUser.Username = model.Username; appUser.Role = model.Role; appUser.Birthday = model.Birthday; db.AppUsers.Add(appUser); db.SaveChanges(); var user = new RAIdentityUser(appUser.Id, model.Username, model.Email); user.PasswordHash = RAIdentityUser.HashPassword(model.Password); IdentityResult result = await UserManager.CreateAsync(user, model.Password); UserManager.AddToRole(user.Id, model.Role); if (!result.Succeeded) { return(GetErrorResult(result)); } return(Ok()); }
public IHttpActionResult PutAppUser(int id, UpdateUserModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != model.Id) { return(BadRequest()); } string username = User.Identity.Name; RAIdentityUser RAUser = db.Users.Get(username); AppUser apUser = db.AppUsers.Get(RAUser.AppUserId); if (model.Id != apUser.Id) { return(BadRequest("You are not authorized.")); } AppUser user = db.AppUsers.Get(model.Id); user.FullName = model.FullName; user.LastName = model.LastName; user.Birthday = model.Birthday; RAUser.PasswordHash = RAIdentityUser.HashPassword(model.Password); db.AppUsers.Update(user); db.Users.Update(RAUser); try { db.Complete(); } catch (DbUpdateConcurrencyException) { if (!AppUserExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public async Task <IHttpActionResult> Register(RegisterBindingModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (model.Role != "AppUser" && model.Role != "Driver") { return(BadRequest()); } AppUser.UserRole userRole; if (model.Role == "AppUser") { userRole = AppUser.UserRole.AppUser; } else { userRole = AppUser.UserRole.Driver; } var appUser = new AppUser() { Email = model.Email, Username = model.Username, FullName = model.FullName, Role = userRole, JMBG = model.JMBG, ContactNumber = model.ContactNumber, Blocked = false, DriverFree = true, Deleted = false }; var user = new RAIdentityUser() { UserName = model.Email, Email = model.Email, AppUser = appUser, PasswordHash = RAIdentityUser.HashPassword(model.Password) }; UserManager.Create(user); UserManager.AddToRole(user.Id, model.Role); return(Ok()); }
public IHttpActionResult ChangeDriver(Driver driver) { RAIdentityUser RAuser2change = db.Users .Where(b => b.UserName == driver.Username) .FirstOrDefault(); if (RAuser2change == null) { return(NotFound()); } Driver driver2change = (Driver)db.AppUsers .Where(b => b.Id == RAuser2change.AppUserId) .FirstOrDefault(); if (driver2change == null) { return(NotFound()); } driver2change.ContactNumber = driver.ContactNumber; driver2change.DriveType = driver.DriveType; driver2change.Email = driver.Email; driver2change.FullName = driver.FullName; driver2change.Gender = driver.Gender; driver2change.DriveType = driver.DriveType; driver2change.LocationId = driver.LocationId; driver2change.CarId = driver.CarId; RAuser2change.PasswordHash = RAIdentityUser.HashPassword(driver.PasswordLogin); try { db.Entry(driver2change).State = EntityState.Modified; db.SaveChanges(); } catch (DbEntityValidationException) { return(BadRequest(ModelState)); } catch (DbUpdateException) { return(BadRequest(ModelState)); } return(Ok(driver2change)); }
public IHttpActionResult ChangeUser(RegisterBindingModel user) { RAIdentityUser RAuser2change = db.Users .Where(b => b.UserName == user.Username) .FirstOrDefault(); if (RAuser2change == null) { return(NotFound()); } AppUser user2change = db.AppUsers .Where(b => b.Id == RAuser2change.AppUserId) .FirstOrDefault(); if (user2change == null) { return(NotFound()); } user2change.ContactNumber = user.ContactNumber; user2change.DriveType = user.DriveType; user2change.Email = user.Email; user2change.FullName = user.FullName; user2change.Gender = user.Gender; user2change.DriveType = user.DriveType; RAuser2change.PasswordHash = RAIdentityUser.HashPassword(user.Password); try { db.Entry(user2change).State = EntityState.Modified; db.SaveChanges(); } catch (DbEntityValidationException) { return(BadRequest(ModelState)); } catch (DbUpdateException) { return(BadRequest(ModelState)); } return(Ok(user2change)); }
public async Task <IHttpActionResult> Register(RegisterBindingModel model) { try { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (UserManager.FindByEmail(model.Email) != null) { return(BadRequest("Username already exists.")); } var user = new RAIdentityUser() { UserName = model.Email, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName, DocumentImage = "", DateOfBirth = model.DateOfBirth, IsApproved = false }; user.PasswordHash = RAIdentityUser.HashPassword(model.Password); IdentityResult addUserResult = await UserManager.CreateAsync(user, model.Password); if (!addUserResult.Succeeded) { return(GetErrorResult(addUserResult)); } IdentityResult addRoleResult = await UserManager.AddToRoleAsync(user.Id, "AppUser"); if (!addRoleResult.Succeeded) { return(GetErrorResult(addRoleResult)); } } catch (DBConcurrencyException) { return(BadRequest()); } NotificationHub.NewUserAccountToApprove(unitOfWork.AccountsForApprove.GetAll().Count()); return(Ok("Account successfully created.")); }
public IHttpActionResult Register(RegisterBindingModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } //TODO:PROVERI OVO!!! AppUser appUser = new AppUser() { Email = model.Email, FullName = model.FullName, DateBirth = model.DateBirth }; var user = new RAIdentityUser() { UserName = model.Email, Email = model.Email, PasswordHash = RAIdentityUser.HashPassword(model.Password), AppUser = appUser }; IdentityResult result = null; var user1 = UserManager.Users.FirstOrDefault(u => u.Email == model.Email); if (user1 == null) { try { result = UserManager.Create(user); UserManager.AddToRole(user.Id, "AppUser"); } catch (DbEntityValidationException e) { } if (!result.Succeeded) { return(GetErrorResult(result)); } return(Ok()); } return(Conflict()); }
public async Task <IHttpActionResult> Register(RegisterBindingModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var appUser = new AppUser() { BirthDay = model.BirthDay, Email = model.Email, FullName = model.FullName, Activated = false, PersonalDocument = null, Rents = new List <Rent>() }; var user = new RAIdentityUser() { AppUser = appUser, PasswordHash = RAIdentityUser.HashPassword(model.Password), UserName = model.Email, Email = model.Email, }; NotificationsHub.NotifyAdmin("New user added!"); IdentityResult result = await UserManager.CreateAsync(user, model.Password); UserManager.AddToRole(user.Id, "AppUser"); var errorMessage = result.Errors.ToList(); if (!result.Succeeded) { return(GetErrorResult(result)); } //userManager.Create(user); //userManager.AddToRole(user.Id, appUser); return(Ok()); }
public async Task <IHttpActionResult> Register(RegisterBindingModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } foreach (AppUser u in unitOfWork.AppUsers.GetAll()) { if (u.Email == model.Email) { return(Unauthorized()); } } var user = new RAIdentityUser() { UserName = model.Email, Email = model.Email, Id = model.Email, AppUser = new AppUser { Birthday = model.Birthday, Email = model.Email, FullName = model.FullName, Rents = new System.Collections.Generic.List <Rent>() } }; user.PasswordHash = RAIdentityUser.HashPassword(model.Password); IdentityResult result = await UserManager.CreateAsync(user, model.Password); if (!result.Succeeded) { return(GetErrorResult(result)); } UserManager.AddToRole(user.Id, "AppUser"); // notification -------------------------------------------------------------- NotificationsHub.NotifyForUser(++AccountCount); return(Ok()); }
protected override void Seed(RentApp.Persistance.RADBContext context) { // This method will be called after migrating to the latest version. // You can use the DbSet<T>.AddOrUpdate() helper extension method // to avoid creating duplicate seed data. E.g. //context.People.AddOrUpdate( // p => p.FullName, // new Person { FullName = "Andrew Peters" }, // new Person { FullName = "Brice Lambson" }, // new Person { FullName = "Rowan Miller" } //); if (!context.Roles.Any(r => r.Name == "Admin")) { var store = new RoleStore <IdentityRole>(context); var manager = new RoleManager <IdentityRole>(store); var role = new IdentityRole { Name = "Admin" }; manager.Create(role); } if (!context.Roles.Any(r => r.Name == "Manager")) { var store = new RoleStore <IdentityRole>(context); var manager = new RoleManager <IdentityRole>(store); var role = new IdentityRole { Name = "Manager" }; manager.Create(role); } if (!context.Roles.Any(r => r.Name == "AppUser")) { var store = new RoleStore <IdentityRole>(context); var manager = new RoleManager <IdentityRole>(store); var role = new IdentityRole { Name = "AppUser" }; manager.Create(role); } context.AppUsers.AddOrUpdate( u => u.FirstName, new AppUser() { Id = 1, FirstName = "Branja" } ); context.AppUsers.AddOrUpdate( p => p.FirstName, new AppUser() { Id = 2, FirstName = "David" } ); context.SaveChanges(); var userStore = new UserStore <RAIdentityUser>(context); var userManager = new UserManager <RAIdentityUser>(userStore); if (!context.Users.Any(u => u.UserName == "Branja")) { var _appUser = context.AppUsers.FirstOrDefault(a => a.FirstName == "Branja"); var user = new RAIdentityUser() { AppUser = _appUser, Id = "Admin_1", UserName = "******", Email = "*****@*****.**", PasswordHash = RAIdentityUser.HashPassword("Branja"), AppUserId = _appUser.Id }; userManager.Create(user); userManager.AddToRole(user.Id, "Admin"); } if (!context.Users.Any(u => u.UserName == "David")) { var _appUser = context.AppUsers.FirstOrDefault(a => a.FirstName == "David"); var user = new RAIdentityUser() { AppUser = _appUser, Id = "Admin_2", UserName = "******", Email = "*****@*****.**", PasswordHash = RAIdentityUser.HashPassword("David"), AppUserId = _appUser.Id }; userManager.Create(user); userManager.AddToRole(user.Id, "Admin"); } context.SaveChanges(); }
protected override void Seed(RentApp.Persistance.RADBContext context) { // This method will be called after migrating to the latest version. // You can use the DbSet<T>.AddOrUpdate() helper extension method // to avoid creating duplicate seed data. E.g. // // context.People.AddOrUpdate( // p => p.FullName, // new Person { FullName = "Andrew Peters" }, // new Person { FullName = "Brice Lambson" }, // new Person { FullName = "Rowan Miller" } // ); // if (!context.Roles.Any(r => r.Name == "Admin")) { var store = new RoleStore <IdentityRole>(context); var manager = new RoleManager <IdentityRole>(store); var role = new IdentityRole { Name = "Admin" }; manager.Create(role); } if (!context.Roles.Any(r => r.Name == "Manager")) { var store = new RoleStore <IdentityRole>(context); var manager = new RoleManager <IdentityRole>(store); var role = new IdentityRole { Name = "Manager" }; manager.Create(role); } if (!context.Roles.Any(r => r.Name == "AppUser")) { var store = new RoleStore <IdentityRole>(context); var manager = new RoleManager <IdentityRole>(store); var role = new IdentityRole { Name = "AppUser" }; manager.Create(role); } context.AppUsers.AddOrUpdate( u => u.FullName, new AppUser() { FullName = "Admin Adminovic" } ); context.AppUsers.AddOrUpdate( p => p.FullName, new AppUser() { FullName = "AppUser AppUserovic", Activated = false, DateOfBirth = DateTime.Today, Email = "gmail", Rents = new System.Collections.Generic.List <Rent>() } ); var userStore = new UserStore <RAIdentityUser>(context); var userManager = new UserManager <RAIdentityUser>(userStore); if (!context.Users.Any(u => u.UserName == "admin")) { var _appUser = context.AppUsers.FirstOrDefault(a => a.FullName == "Admin Adminovic"); var user = new RAIdentityUser() { Id = "admin", UserName = "******", Email = "*****@*****.**", PasswordHash = RAIdentityUser.HashPassword("Admin-1"), AppUserId = _appUser.Id }; userManager.Create(user); userManager.AddToRole(user.Id, "Admin"); } if (!context.Users.Any(u => u.UserName == "appu")) { var _appUser = context.AppUsers.FirstOrDefault(a => a.FullName == "AppUser AppUserovic"); var user = new RAIdentityUser() { Id = "appu", UserName = "******", Email = "*****@*****.**", PasswordHash = RAIdentityUser.HashPassword("appu"), AppUserId = _appUser.Id }; userManager.Create(user); userManager.AddToRole(user.Id, "AppUser"); } }
protected override void Seed(RentApp.Persistance.RADBContext context) { //debug //if (System.Diagnostics.Debugger.IsAttached == false) //{ // System.Diagnostics.Debugger.Launch(); //} //((System.Data.Entity.Validation.DbEntityValidationException)$exception).EntityValidationErrors // This method will be called after migrating to the latest version. // You can use the DbSet<T>.AddOrUpdate() helper extension method // to avoid creating duplicate seed data. E.g. // // context.People.AddOrUpdate( // p => p.FullName, // new Person { FullName = "Andrew Peters" }, // new Person { FullName = "Brice Lambson" }, // new Person { FullName = "Rowan Miller" } // ); // //if (!context.Roles.Any(r => r.Name == "Admin")) //{ // var store = new RoleStore<IdentityRole>(context); // var manager = new RoleManager<IdentityRole>(store); // var role = new IdentityRole { Name = "Admin" }; // manager.Create(role); //} //if (!context.Roles.Any(r => r.Name == "Manager")) //{ // var store = new RoleStore<IdentityRole>(context); // var manager = new RoleManager<IdentityRole>(store); // var role = new IdentityRole { Name = "Manager" }; // manager.Create(role); //} //if (!context.Roles.Any(r => r.Name == "AppUser")) //{ // var store = new RoleStore<IdentityRole>(context); // var manager = new RoleManager<IdentityRole>(store); // var role = new IdentityRole { Name = "AppUser" }; // manager.Create(role); //} //context.AppUsers.AddOrUpdate( // u => u.FullName, // new AppUser() { FullName = "Admin Adminovic" } //); //context.AppUsers.AddOrUpdate( // p => p.FullName, // new AppUser() { FullName = "AppUser AppUserovic" } //); //context.SaveChanges(); //var userStore = new UserStore<RAIdentityUser>(context); //var userManager = new UserManager<RAIdentityUser>(userStore); //if (!context.Users.Any(u => u.UserName == "admin")) //{ // var _appUser = context.AppUsers.FirstOrDefault(a => a.FullName == "Admin Adminovic"); // var user = new RAIdentityUser() { Id = "admin", UserName = "******", Email = "*****@*****.**", PasswordHash = RAIdentityUser.HashPassword("admin"), AppUserId = _appUser.Id }; // userManager.Create(user); // userManager.AddToRole(user.Id, "Admin"); //} //if (!context.Users.Any(u => u.UserName == "appu")) //{ // var _appUser = context.AppUsers.FirstOrDefault(a => a.FullName == "AppUser AppUserovic"); // var user = new RAIdentityUser() { Id = "appu", UserName = "******", Email = "*****@*****.**", PasswordHash = RAIdentityUser.HashPassword("appu"), AppUserId = _appUser.Id }; // userManager.Create(user); // userManager.AddToRole(user.Id, "AppUser"); //} if (!context.Roles.Any(r => r.Name == "Admin")) { var store = new RoleStore <IdentityRole>(context); var manager = new RoleManager <IdentityRole>(store); var role = new IdentityRole { Name = "Admin" }; manager.Create(role); } if (!context.Roles.Any(r => r.Name == "Manager")) { var store = new RoleStore <IdentityRole>(context); var manager = new RoleManager <IdentityRole>(store); var role = new IdentityRole { Name = "manager" }; manager.Create(role); } if (!context.Roles.Any(r => r.Name == "AppUser")) { var store = new RoleStore <IdentityRole>(context); var manager = new RoleManager <IdentityRole>(store); var role = new IdentityRole { Name = "AppUser" }; manager.Create(role); } context.AppUsers.AddOrUpdate( u => u.FullName, new AppUser() { FullName = "Admin Adminovic", Email = "*****@*****.**", BirthDate = DateTime.Parse("1/1/2000"), DocumentPicture = "123" } ); context.AppUsers.AddOrUpdate( u => u.FullName, new AppUser() { FullName = "Manager", Email = "*****@*****.**", BirthDate = DateTime.Parse("1/1/2000"), DocumentPicture = "123" } ); context.AppUsers.AddOrUpdate( p => p.FullName, new AppUser() { FullName = "AppUser AppUserovic", Email = "*****@*****.**", BirthDate = DateTime.Parse("1/1/2001"), DocumentPicture = "11" } ); //rentServices // context.RentServices.AddOrUpdate( // p => p.Name, // new RentService() { Name = "Rent1", Email = "*****@*****.**", Description="Description of the description",Grade=4,Logo="1234" } // ); // context.RentServices.AddOrUpdate( // p => p.Name, // new RentService() { Name = "Rent2", Email = "*****@*****.**", Description = "Description of the description", Grade = 1, Logo = "1234" } // ); // context.RentServices.AddOrUpdate( // p => p.Name, // new RentService() { Name = "Rent3", Email = "*****@*****.**", Description = "Description of the description", Grade = 5, Logo = "1234" } //); //type // context.TypesOfVehicles.AddOrUpdate( // p => p.TypeId, // new TypeOfVehicle() { Type = "type1" } // ); context.TypesOfVehicles.AddOrUpdate( // p => p.TypeId, // new TypeOfVehicle(){ Type = "type1" } // ); context.TypesOfVehicles.AddOrUpdate( // p => p.TypeId, // new TypeOfVehicle() { Type = "type1" } // ); // context.Vehicles.AddOrUpdate( // p => p.VehicleId, // new Vehicle() // { // Available = true, // Manufacturer = "*****@*****.**", // Model = "Description of the description", // Description = "jdfgdhfdhgf", // Pictures = "1234", // RentServiceId = 1, // YearOfManufacturing = "2010", // TypeId = 1, // } // ); // context.Vehicles.AddOrUpdate( // p => p.VehicleId, // new Vehicle() // { // Available = true, // Manufacturer = "*****@*****.**", // Model = "Description of the description2", // Description = "jdfgdhfdhgf", // Pictures = "1234", // RentServiceId = 1, // YearOfManufacturing = "2015", // TypeId = 2, // } // ); // context.Vehicles.AddOrUpdate( // p => p.VehicleId, // new Vehicle() // { // Available = false, // Manufacturer = "*****@*****.**", // Model = "Description of the description3", // Description = "jdfgdhfdhgf", // Pictures = "1234", // RentServiceId = 2, // YearOfManufacturing = "2013", // TypeId = 3, // } //); try { context.SaveChanges(); } //catch (DbEntityValidationException ex) //{ // StringBuilder sb = new StringBuilder(); // foreach (var failure in ex.EntityValidationErrors) // { // sb.AppendFormat("{0} failed validation\n", failure.Entry.Entity.GetType()); // foreach (var error in failure.ValidationErrors) // { // sb.AppendFormat("- {0} : {1}", error.PropertyName, error.ErrorMessage); // sb.AppendLine(); // } // } // throw new DbEntityValidationException( // "Entity Validation Failed - errors follow:\n" + // sb.ToString(), ex // ); // Add the original exception as the innerException //} catch (DbEntityValidationException dbEx) { foreach (var validationErrors in dbEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { System.Diagnostics.Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage); } } } // context.SaveChanges(); var userStore = new UserStore <RAIdentityUser>(context); var userManager = new UserManager <RAIdentityUser>(userStore); if (!context.Users.Any(u => u.UserName == "admin")) { var _appUser = context.AppUsers.FirstOrDefault(a => a.FullName == "Admin Adminovic"); var user = new RAIdentityUser() { Id = "admin", UserName = "******", Email = "*****@*****.**", PasswordHash = RAIdentityUser.HashPassword("admin"), AppUserId = _appUser.UserId }; userManager.Create(user); userManager.AddToRole(user.Id, "Admin"); } if (!context.Users.Any(u => u.UserName == "manager")) { var _appUser = context.AppUsers.FirstOrDefault(a => a.FullName == "Manager"); var user = new RAIdentityUser() { Id = "manager", UserName = "******", Email = "*****@*****.**", PasswordHash = RAIdentityUser.HashPassword("manager"), AppUserId = _appUser.UserId }; userManager.Create(user); userManager.AddToRole(user.Id, "Manager"); } if (!context.Users.Any(u => u.UserName == "appu")) { var _appUser = context.AppUsers.FirstOrDefault(a => a.FullName == "AppUser AppUserovic"); var user = new RAIdentityUser() { Id = "appu", UserName = "******", Email = "*****@*****.**", PasswordHash = RAIdentityUser.HashPassword("appu"), AppUserId = _appUser.UserId }; userManager.Create(user); userManager.AddToRole(user.Id, "AppUser"); } }
protected override void Seed(RentApp.Persistance.RADBContext context) { // This method will be called after migrating to the latest version. // You can use the DbSet<T>.AddOrUpdate() helper extension method // to avoid creating duplicate seed data. E.g. // // context.People.AddOrUpdate( // p => p.FullName, // new Person { FullName = "Andrew Peters" }, // new Person { FullName = "Brice Lambson" }, // new Person { FullName = "Rowan Miller" } // ); // if (!context.Roles.Any(r => r.Name == "Admin")) { var store = new RoleStore <IdentityRole>(context); var manager = new RoleManager <IdentityRole>(store); var role = new IdentityRole { Name = "Admin" }; manager.Create(role); } if (!context.Roles.Any(r => r.Name == "Manager")) { var store = new RoleStore <IdentityRole>(context); var manager = new RoleManager <IdentityRole>(store); var role = new IdentityRole { Name = "Manager" }; manager.Create(role); } if (!context.Roles.Any(r => r.Name == "AppUser")) { var store = new RoleStore <IdentityRole>(context); var manager = new RoleManager <IdentityRole>(store); var role = new IdentityRole { Name = "AppUser" }; manager.Create(role); } context.AppUsers.AddOrUpdate( u => u.FullName, new AppUser() { FullName = "Admin Adminovic" } ); context.AppUsers.AddOrUpdate( p => p.FullName, new AppUser() { FullName = "AppUser AppUserovic" } ); context.SaveChanges(); var userStore = new UserStore <RAIdentityUser>(context); var userManager = new UserManager <RAIdentityUser>(userStore); if (!context.Users.Any(u => u.UserName == "admin")) { var _appUser = context.AppUsers.FirstOrDefault(a => a.FullName == "Admin Adminovic"); var user = new RAIdentityUser() { Id = "admin", UserName = "******", Email = "*****@*****.**", PasswordHash = RAIdentityUser.HashPassword("admin"), AppUserId = _appUser.Id }; userManager.Create(user); userManager.AddToRole(user.Id, "Admin"); } if (!context.Users.Any(u => u.UserName == "appu")) { var _appUser = context.AppUsers.FirstOrDefault(a => a.FullName == "AppUser AppUserovic"); var user = new RAIdentityUser() { Id = "appu", UserName = "******", Email = "*****@*****.**", PasswordHash = RAIdentityUser.HashPassword("appu"), AppUserId = _appUser.Id }; userManager.Create(user); userManager.AddToRole(user.Id, "AppUser"); } Service ser = new Service(); BranchOffice br = new BranchOffice(); Vehicle v = new Vehicle(); ser.Name = "Service 1"; ser.Email = "*****@*****.**"; ser.Description = "ser_1_decs"; ser.AppUserId = 2; context.Services.AddOrUpdate( s => s.Name, ser ); context.SaveChanges(); v.TypeVehicle = "Hatchback"; v.Producer = "Peugeot"; v.Model = "307"; v.ProductionDate = DateTime.Now; v.ServiceId = ser.Id; v.Pirce = 25; br.Addres = "br_1_addr"; br.X = 555555; br.Y = 666666; br.ServiceId = ser.Id; context.BranchOffices.AddOrUpdate( s => s.Addres, br ); context.Vehicles.AddOrUpdate( s => s.Model, v ); context.SaveChanges(); }
public async Task <IHttpActionResult> Register(RegisterBindingModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var appUser = new AppUser(); appUser.Email = model.Email; appUser.FullName = model.FullName; appUser.ContactNumber = model.ContactNumber; appUser.DriveType = model.DriveType; appUser.Gender = model.Gender; appUser.Role = model.Role; appUser.SSN = model.SSN; appUser.Username = model.Username; //check all fields var user = new RAIdentityUser() { Id = model.Email, UserName = model.Username, Email = model.Email, AppUser = appUser, PasswordHash = RAIdentityUser.HashPassword(model.Password) }; IdentityResult result = await UserManager.CreateAsync(user); UserManager.AddToRole(user.Id, model.Role.ToString()); if (!result.Succeeded) { return(GetErrorResult(result)); } return(Ok()); }
public async Task <IHttpActionResult> Register(RegisterBindingModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } AppUser appUser = new AppUser(); appUser.Email = model.Email; appUser.Activated = false; appUser.FullName = model.FullName; appUser.Birthday = DateTime.Parse(model.Date); appUser.Rents = new List <Rent>(); var user = new RAIdentityUser() { UserName = model.Email, Email = model.Email, AppUser = appUser, PasswordHash = RAIdentityUser.HashPassword(model.Password) }; IdentityResult result = await UserManager.CreateAsync(user, model.Password); if (!result.Succeeded) { return(GetErrorResult(result)); } UserManager.AddToRole(user.Id, "AppUser"); // Dajemo rolu return(Ok()); }
public IHttpActionResult Register(RegisterBindingModel model) { //if(model.IsManager) if (!ModelState.IsValid) { return(BadRequest(ModelState)); } AppUser appUser = new AppUser() { FullName = model.FullName, Birthday = DateTime.Now, LastName = model.LastName, IsManagerAllowed = false, IsUserConfirmed = false }; var user = new RAIdentityUser() { Id = model.Email, UserName = model.Email, Email = model.Email, AppUser = appUser, PasswordHash = RAIdentityUser.HashPassword(model.Password) }; IdentityResult result = null; try { result = UserManager.Create(user); } catch (DbEntityValidationException e) { return(BadRequest(ModelState)); } if (model.IsManager) { UserManager.AddToRole(user.Id, "Manager"); } else { UserManager.AddToRole(user.Id, "AppUser"); } if (!result.Succeeded) { return(GetErrorResult(result)); } return(Ok()); }
protected override void Seed(RentApp.Persistance.RADBContext context) { // This method will be called after migrating to the latest version. // You can use the DbSet<T>.AddOrUpdate() helper extension method // to avoid creating duplicate seed data. E.g. // // context.People.AddOrUpdate( // p => p.FullName, // new Person { FullName = "Andrew Peters" }, // new Person { FullName = "Brice Lambson" }, // new Person { FullName = "Rowan Miller" } // ); // if (!context.Roles.Any(r => r.Name == "Admin")) { var store = new RoleStore <IdentityRole>(context); var manager = new RoleManager <IdentityRole>(store); var role = new IdentityRole { Name = "Admin" }; manager.Create(role); } if (!context.Roles.Any(r => r.Name == "Manager")) { var store = new RoleStore <IdentityRole>(context); var manager = new RoleManager <IdentityRole>(store); var role = new IdentityRole { Name = "Manager" }; manager.Create(role); } if (!context.Roles.Any(r => r.Name == "AppUser")) { var store = new RoleStore <IdentityRole>(context); var manager = new RoleManager <IdentityRole>(store); var role = new IdentityRole { Name = "AppUser" }; manager.Create(role); } context.AppUsers.AddOrUpdate( u => u.FullName, new AppUser() { FullName = "Admin Adminovic" } ); context.AppUsers.AddOrUpdate( p => p.FullName, new AppUser() { FullName = "AppUser AppUserovic" } ); context.AppUsers.AddOrUpdate( m => m.FullName, new AppUser() { FullName = "Manager Managerovic" } ); //var tov1 = new TypeOfVehicle() { Name = "Karavan" }; //var tov2 = new TypeOfVehicle() { Name = "Limuzina" }; //var v1 = new Vehicle() { Manufactor = "Zastava", Model = "101", PricePerHour = 10.4m, Type = tov1, Year = 1969 }; //var v2 = new Vehicle() { Manufactor = "Zastava", Model = "Yugo Koral", PricePerHour = 10.4m, Type = tov2, Year = 1988 }; //var b1 = new Branch() { Address = "Sime Solaje 24", Latitude = 123424.2, Longitude = 123232.4 }; //var s1 = new Service() { Name = "Zastava rent", Email = "*****@*****.**", Description = "Nepobedivi", Branches = new List<Branch>() { b1 }, Vehicles = new List<Vehicle>() { v1, v2 } }; //context.Services.Add(s1); //SaveChanges(context); var userStore = new UserStore <RAIdentityUser>(context); var userManager = new UserManager <RAIdentityUser>(userStore); if (!context.Users.Any(u => u.UserName == "admin")) { var _appUser = context.AppUsers.FirstOrDefault(a => a.FullName == "Admin Adminovic"); var user = new RAIdentityUser() { Id = "admin", UserName = "******", Email = "*****@*****.**", PasswordHash = RAIdentityUser.HashPassword("admin"), AppUserId = _appUser.Id }; userManager.Create(user); userManager.AddToRole(user.Id, "Admin"); } if (!context.Users.Any(u => u.UserName == "appu")) { var _appUser = context.AppUsers.FirstOrDefault(a => a.FullName == "AppUser AppUserovic"); var user = new RAIdentityUser() { Id = "appu", UserName = "******", Email = "*****@*****.**", PasswordHash = RAIdentityUser.HashPassword("appu"), AppUserId = _appUser.Id }; userManager.Create(user); userManager.AddToRole(user.Id, "AppUser"); } if (!context.Users.Any(u => u.UserName == "manager")) { var _appUser = context.AppUsers.FirstOrDefault(a => a.FullName == "Manager Managerovic"); var user = new RAIdentityUser() { Id = "manager", UserName = "******", Email = "*****@*****.**", PasswordHash = RAIdentityUser.HashPassword("manager"), AppUserId = _appUser.Id }; userManager.Create(user); userManager.AddToRole(user.Id, "Manager"); } }
protected override void Seed(RentApp.Persistance.RADBContext context) { // This method will be called after migrating to the latest version. // You can use the DbSet<T>.AddOrUpdate() helper extension method // to avoid creating duplicate seed data. E.g. // // context.People.AddOrUpdate( // p => p.FullName, // new Person { FullName = "Andrew Peters" }, // new Person { FullName = "Brice Lambson" }, // new Person { FullName = "Rowan Miller" } // ); // if (!context.Roles.Any(r => r.Name == "Admin")) { var store = new RoleStore <IdentityRole>(context); var manager = new RoleManager <IdentityRole>(store); var role = new IdentityRole { Name = "Admin" }; manager.Create(role); } if (!context.Roles.Any(r => r.Name == "Manager")) { var store = new RoleStore <IdentityRole>(context); var manager = new RoleManager <IdentityRole>(store); var role = new IdentityRole { Name = "Manager" }; manager.Create(role); } if (!context.Roles.Any(r => r.Name == "AppUser")) { var store = new RoleStore <IdentityRole>(context); var manager = new RoleManager <IdentityRole>(store); var role = new IdentityRole { Name = "AppUser" }; manager.Create(role); } context.AppUsers.AddOrUpdate( u => u.FullName, new AppUser() { FullName = "Admin Adminovic" } ); context.AppUsers.AddOrUpdate( u => u.FullName, new AppUser() { FullName = "Menadzer Menadzerovic", Email = "*****@*****.**", Birthday = DateTime.Now, Activated = true } ); context.AppUsers.AddOrUpdate( p => p.FullName, new AppUser() { FullName = "AppUser AppUserovic", Email = "*****@*****.**", Id = 12345 } ); context.AppUsers.AddOrUpdate( s => s.FullName, new AppUser() { FullName = "Sofija Ilic", Email = "*****@*****.**", Id = 1234567890 } ); //BranchOffice bo = new BranchOffice() { Address = "Random adresa", Latitude = 45.267136, Longitude = 19.833549, Logo = "logo.png", Name = "Office1" }; //Vehicle vozilo = new Vehicle(); //VehicleType vt = new VehicleType() { Name = "Limuzina" }; //vozilo.Model = "Audi"; //vozilo.Manufactor = "Proizvodjac1"; //vozilo.Year = 2015; //vozilo.Description = "asdasdas"; //vozilo.Type = vt; //vozilo.PricePerHour = 10; //vozilo.Unavailable = false; //vozilo.Images = new List<string>() { "slika1.png" }; //vt.Vehicles = new List<Vehicle>() { vozilo }; //Service s1 = new Service() { BranchOffices = new List<BranchOffice>() { bo }, Email = "*****@*****.**", Description = "asdasdasd", Name = "test name", Logo = "logo.png", Vehicles = new List<Vehicle>() { vozilo } }; //Rent r1 = new Rent() { BranchOffice = bo, Start = DateTime.Now, End = DateTime.Now, Vehicle = vozilo }; //AppUser appUser = new AppUser() { FullName = "Pera Petrovic", Email = "*****@*****.**", Birthday = DateTime.Now, Image = "slika.jpg", Activated = false, Rents = new List<Rent>() { r1 } }; //VehicleType vt = new VehicleType() { Name = "Limuzina" }; //Vehicle v1 = new Vehicle() { Model = "Punto", Description = "asdasdasd", Images = new List<string>() { "slika.jpg" }, Manufactor = "Fiat", PricePerHour = 10, Type = vt, Unavailable = false, Year = 2015 }; //vt.Vehicles = new List<Vehicle>() { v1 }; //BranchOffice bo = new BranchOffice() { Address = "Adresa1", Latitude = 45.123123, Longitude = 19.123123, Logo = "logo.jpg", Name = "Office1" }; //Rent r1 = new Rent() { BranchOffice = bo, Start = DateTime.Now, End = DateTime.Now, Vehicle = v1 }; //AppUser appuser = new AppUser() { Activated = false, Birthday = DateTime.Now, FullName = "User Userovic", Email = "*****@*****.**", Image = "slika.jpg", Rents = new List<Rent>() { r1 } }; //Service s1 = new Service() { BranchOffices = new List<BranchOffice>() { bo }, Description = "adasdasd", Email = "*****@*****.**", Logo = "logo1.png", Name = "Service1", Vehicles = new List<Vehicle>() { v1 } }; //context.AppUsers.AddOrUpdate( // e => e.Email, // appuser //); //context.BranchOffices.AddOrUpdate( // b => b.Name, // bo //); //context.VehicleTypes.AddOrUpdate( // v => v.Name, // vt //); //context.Vehicles.AddOrUpdate( // v => v.Model, // v1 //); //context.Services.AddOrUpdate( // s => s.Name, // s1 //); //context.Rents.AddOrUpdate( // r => r.Id, // r1 //); SaveChanges(context); var userStore = new UserStore <RAIdentityUser>(context); var userManager = new UserManager <RAIdentityUser>(userStore); if (!context.Users.Any(u => u.UserName == "admin")) { var _appUser = context.AppUsers.FirstOrDefault(a => a.FullName == "Admin Adminovic"); var user = new RAIdentityUser() { Id = "admin", UserName = "******", Email = "*****@*****.**", PasswordHash = RAIdentityUser.HashPassword("admin"), AppUserId = _appUser.Id }; userManager.Create(user); userManager.AddToRole(user.Id, "Admin"); } if (!context.Users.Any(u => u.UserName == "menadzerko")) { var _appUser = context.AppUsers.FirstOrDefault(a => a.FullName == "Menadzer Menadzerovic"); var user = new RAIdentityUser() { Id = "*****@*****.**", UserName = "******", Email = "*****@*****.**", PasswordHash = RAIdentityUser.HashPassword("menadzer"), AppUserId = _appUser.Id }; userManager.Create(user); userManager.AddToRole(user.Id, "Manager"); } if (!context.Users.Any(u => u.UserName == "appu")) { var _appUser = context.AppUsers.FirstOrDefault(a => a.FullName == "AppUser AppUserovic"); var user = new RAIdentityUser() { Id = "appu", UserName = "******", Email = "*****@*****.**", PasswordHash = RAIdentityUser.HashPassword("appu"), AppUserId = _appUser.Id }; userManager.Create(user); userManager.AddToRole(user.Id, "AppUser"); } }
protected override void Seed(RentApp.Persistance.RADBContext context) { // This method will be called after migrating to the latest version. // You can use the DbSet<T>.AddOrUpdate() helper extension method // to avoid creating duplicate seed data. E.g. // // context.People.AddOrUpdate( // p => p.FullName, // new Person { FullName = "Andrew Peters" }, // new Person { FullName = "Brice Lambson" }, // new Person { FullName = "Rowan Miller" } // ); // if (!context.Roles.Any(r => r.Name == "Admin")) { var store = new RoleStore <IdentityRole>(context); var manager = new RoleManager <IdentityRole>(store); var role = new IdentityRole { Name = "Admin" }; manager.Create(role); } if (!context.Roles.Any(r => r.Name == "Manager")) { var store = new RoleStore <IdentityRole>(context); var manager = new RoleManager <IdentityRole>(store); var role = new IdentityRole { Name = "Manager" }; manager.Create(role); } if (!context.Roles.Any(r => r.Name == "AppUser")) { var store = new RoleStore <IdentityRole>(context); var manager = new RoleManager <IdentityRole>(store); var role = new IdentityRole { Name = "AppUser" }; manager.Create(role); } context.AppUsers.AddOrUpdate( p => p.Id, new AppUser { Approved = false, BirthDate = new DateTime(1994, 8, 30), CreateService = true, Surname = "Adminko", LoggedIn = false, Name = "Admin", Username = "******", Contact = "060000000" } ); context.AppUsers.AddOrUpdate( p => p.Id, new AppUser { Approved = false, BirthDate = new DateTime(1994, 8, 30), CreateService = true, Surname = "Userovic", LoggedIn = false, Name = "AppUser", Username = "******", Contact = "060000000" } ); context.SaveChanges(); var userStore = new UserStore <RAIdentityUser>(context); var userManager = new UserManager <RAIdentityUser>(userStore); if (!context.Users.Any(u => u.UserName == "admin")) { var _appUser = context.AppUsers.FirstOrDefault(a => a.Username == "admin"); var user = new RAIdentityUser() { Id = "admin", UserName = "******", Email = "*****@*****.**", PasswordHash = RAIdentityUser.HashPassword("admin"), AppUserId = _appUser.Id }; userManager.Create(user); userManager.AddToRole(user.Id, "Admin"); } if (!context.Users.Any(u => u.UserName == "appUser")) { var _appUser = context.AppUsers.FirstOrDefault(a => a.Username == "appUser"); var user = new RAIdentityUser() { Id = "appu", UserName = "******", Email = "*****@*****.**", PasswordHash = RAIdentityUser.HashPassword("appu"), AppUserId = _appUser.Id }; userManager.Create(user); userManager.AddToRole(user.Id, "AppUser"); } }
public IHttpActionResult RegisterDriver(RegisterDriverBindingModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (model.Role != "AppUser" && model.Role != "Driver") { return(BadRequest()); } AppUser.UserRole userRole; if (model.Role == "AppUser") { userRole = AppUser.UserRole.AppUser; } else { userRole = AppUser.UserRole.Driver; } var result = unitOfWork.AppUsers.Find(u => u.Username == model.Username); if (result.Count() != 0) { return(BadRequest("Username not unique")); } var resultE = unitOfWork.AppUsers.Find(u => u.Email == model.Email); if (resultE.Count() != 0) { return(BadRequest("Email has account")); } var appUser = new AppUser() { Email = model.Email, FullName = model.FullName, Gender = model.Gender, Username = model.Username, Role = userRole, JMBG = model.JMBG, ContactNumber = model.ContactNumber, Blocked = false, DriverFree = true, Deleted = false }; var user = new RAIdentityUser() { UserName = model.Email, Email = model.Email, AppUser = appUser, PasswordHash = RAIdentityUser.HashPassword(model.Password) }; UserManager.Create(user); UserManager.AddToRole(user.Id, model.Role); CarType ct; if (model.CarType == "Standard") { ct = CarType.Standard; } else { ct = CarType.Combi; } var resultT = unitOfWork.Cars.Find(u => u.TaxiNumber == model.TaxiNumber); if (resultT.Count() != 0) { return(BadRequest("TaxiNumber not unique")); } Car newCar = new Car() { RegistrationPlate = model.RegistrationPlate, YearMade = model.YearMade, TaxiNumber = model.TaxiNumber, CarType = ct }; newCar.Driver = user.AppUser; newCar.AppUserID = user.AppUser.Id; unitOfWork.Cars.Add(newCar); unitOfWork.Complete(); return(Ok()); }
/// <summary> /// /// </summary> /// <param name="context"></param> /// protected override void Seed(RentApp.Persistance.RADBContext context) { // This method will be called after migrating to the latest version. // You can use the DbSet<T>.AddOrUpdate() helper extension method // to avoid creating duplicate seed data. E.g. // // context.People.AddOrUpdate( // p => p.FullName, // new Person { FullName = "Andrew Peters" }, // new Person { FullName = "Brice Lambson" }, // new Person { FullName = "Rowan Miller" } // ); // if (!context.Roles.Any(r => r.Name == "Admin")) { var store = new RoleStore <IdentityRole>(context); var manager = new RoleManager <IdentityRole>(store); var role = new IdentityRole { Name = "Admin" }; manager.Create(role); } if (!context.Roles.Any(r => r.Name == "Manager")) { var store = new RoleStore <IdentityRole>(context); var manager = new RoleManager <IdentityRole>(store); var role = new IdentityRole { Name = "Manager" }; manager.Create(role); } if (!context.Roles.Any(r => r.Name == "AppUser")) { var store = new RoleStore <IdentityRole>(context); var manager = new RoleManager <IdentityRole>(store); var role = new IdentityRole { Name = "AppUser" }; manager.Create(role); } context.AppUsers.AddOrUpdate( u => u.FullName, new AppUser() { FullName = "Admin Adminovic" } ); context.AppUsers.AddOrUpdate( p => p.FullName, new AppUser() { FullName = "AppUser AppUserovic" } ); //novi entiteti /* var T1 = new TypeOfVehicle() { Name = "Limuzina" }; * var s1 = new Service() { Name = "Service1", Description = "First Service", Email = "*****@*****.**", Logo = "" }; * var V1 = new Vehicle() { Model = "A1", Manufactor = "Audi", Year = new DateTime(2018, 1, 1), Description = "First vehicle", PricePerHour = 100, Unvailable = false }; * V1.Type = T1; * var b1 = new Branch() { Address = "Bulevar Oslobodjenja 2", Logo = "", Latitude = 1, Longitude = 2 }; * var R1 = new Rent() { Start = new DateTime(2018, 3, 4), End = new DateTime(2018, 3, 11) }; * * R1.Branch = b1; * R1.Vehicle = V1; * * s1.Branches = new List<Branch>(); * s1.Branches.Add(b1); * s1.Vehicles = new List<Vehicle>(); * s1.Vehicles.Add(V1); * * context.Services.AddOrUpdate(p => p.Name,s1); * context.Branches.AddOrUpdate(p => p.Address,b1); * context.TypeOfVehicles.AddOrUpdate(p => p.Name,T1); * context.Vehicles.AddOrUpdate(p => p.Model,V1); * context.Rents.AddOrUpdate(p => p.Id,R1);*/ SaveChanges(context); var userStore = new UserStore <RAIdentityUser>(context); var userManager = new UserManager <RAIdentityUser>(userStore); if (!context.Users.Any(u => u.UserName == "admin")) { var _appUser = context.AppUsers.FirstOrDefault(a => a.FullName == "Admin Adminovic"); var user = new RAIdentityUser() { Id = "admin", UserName = "******", Email = "*****@*****.**", PasswordHash = RAIdentityUser.HashPassword("admin"), AppUserId = _appUser.Id }; userManager.Create(user); userManager.AddToRole(user.Id, "Admin"); } if (!context.Users.Any(u => u.UserName == "appu")) { var _appUser = context.AppUsers.FirstOrDefault(a => a.FullName == "AppUser AppUserovic"); var user = new RAIdentityUser() { Id = "appu", UserName = "******", Email = "*****@*****.**", PasswordHash = RAIdentityUser.HashPassword("appu"), AppUserId = _appUser.Id }; userManager.Create(user); userManager.AddToRole(user.Id, "AppUser"); } }
public async Task <IHttpActionResult> Register(RegisterBindingModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } AppUser appUser = new AppUser() { FullName = model.FullName, DateOfBirth = DateTime.Parse(model.DateOfBirth) }; var user = new RAIdentityUser() { UserName = model.Email, Email = model.Email, AppUser = appUser, PasswordHash = RAIdentityUser.HashPassword(model.Password) }; IdentityResult result = await UserManager.CreateAsync(user, model.Password); UserManager.AddToRole(user.Id, model.Role); if (!result.Succeeded) { return(GetErrorResult(result)); } return(Ok()); }
public async Task <IHttpActionResult> Register(RegisterBindingModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } AppUser appUser = new AppUser() { FullName = model.FullName, Email = model.Email, BirthDate = model.BirthDate }; var user = new RAIdentityUser() { UserName = model.Email, Email = model.Email, AppUser = appUser, PasswordHash = RAIdentityUser.HashPassword(model.Password) }; IdentityResult result = await UserManager.CreateAsync(user, model.Password); if (!result.Succeeded) { return(GetErrorResult(result)); } UserManager.AddToRole(user.Id, "AppUser"); NotificationsHub.NotifyAdmin("New User was registered"); return(Ok()); }
public async Task <IHttpActionResult> Register(RegisterBindingModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var appUser = new AppUser() { Email = model.Email, FullName = model.Name + " " + model.LastName, Birthday = model.Birthday }; var user = new RAIdentityUser() { Id = model.Email, UserName = model.Email, Email = model.Email, AppUser = appUser, PasswordHash = RAIdentityUser.HashPassword(model.Password) }; UserManager.Create(user); if (model.Role) { UserManager.AddToRole(user.Id, "Manager"); } else { UserManager.AddToRole(user.Id, "AppUser"); } return(Ok()); }
public async Task <IHttpActionResult> Register(RegisterBindingModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } foreach (AppUser u in unitOfWork.AppUser.GetAll()) { if (u.Email == model.Email) { return(Unauthorized()); } } var appuser = new AppUser() { FullName = model.FullName, Email = model.Email, Birthday = model.DateOfBirth, Activated = false, PersonalDocument = null, Rents = new List <Rent>() }; var user = new RAIdentityUser() { UserName = model.Email, Email = model.Email, AppUser = appuser, PasswordHash = RAIdentityUser.HashPassword(model.Password) }; IdentityResult result = await UserManager.CreateAsync(user, model.Password); if (!result.Succeeded) { return(Content(System.Net.HttpStatusCode.BadRequest, "BadRequest!")); } UserManager.AddToRole(user.Id, "AppUser"); NotificationsHub.NotifyAdmin("New user added!"); return(Ok()); }
protected override void Seed(RentApp.Persistance.RADBContext context) { // This method will be called after migrating to the latest version. // You can use the DbSet<T>.AddOrUpdate() helper extension method // to avoid creating duplicate seed data. E.g. // // context.People.AddOrUpdate( // p => p.FullName, // new Person { FullName = "Andrew Peters" }, // new Person { FullName = "Brice Lambson" }, // new Person { FullName = "Rowan Miller" } // ); // if (!context.Roles.Any(r => r.Name == "Admin")) { var store = new RoleStore <IdentityRole>(context); var manager = new RoleManager <IdentityRole>(store); var role = new IdentityRole { Name = "Admin" }; manager.Create(role); } if (!context.Roles.Any(r => r.Name == "Manager")) { var store = new RoleStore <IdentityRole>(context); var manager = new RoleManager <IdentityRole>(store); var role = new IdentityRole { Name = "Manager" }; manager.Create(role); } if (!context.Roles.Any(r => r.Name == "AppUser")) { var store = new RoleStore <IdentityRole>(context); var manager = new RoleManager <IdentityRole>(store); var role = new IdentityRole { Name = "AppUser" }; manager.Create(role); } context.AppUsers.AddOrUpdate( u => u.FullName, new AppUser() { FullName = "Admin Adminovic" } ); context.AppUsers.AddOrUpdate( p => p.FullName, new AppUser() { FullName = "AppUser AppUserovic" } ); Branch b = new Branch(); Service s1 = new Service(); Service s2 = new Service(); Vehicle v1 = new Vehicle(); Vehicle v2 = new Vehicle(); Rent rnt = new Rent(); TypeOfVehicle typeofv = new TypeOfVehicle(); typeofv.Name = "Caravan"; v1.Manufactor = "Golf"; v1.Model = "5"; v1.PricePerHour = 10; v1.Unavailable = false; v1.Year = 2008; v1.Type = typeofv; typeofv.Vehicles = new System.Collections.Generic.List <Vehicle>(); typeofv.Vehicles.Add(v1); typeofv.Name = "Cabriolet"; v2.Manufactor = "Peugeot"; v2.Model = "206"; v2.PricePerHour = 12; v2.Unavailable = false; v2.Year = 2015; v2.Type = typeofv; typeofv.Vehicles = new System.Collections.Generic.List <Vehicle>(); typeofv.Vehicles.Add(v2); b.Address = "Ul.1/23"; b.Latitude = 2222; b.Longitude = 3333; s1.Name = "Service 1"; s1.Email = "*****@*****.**"; s1.Description = "servis s1"; s1.Branches = new System.Collections.Generic.List <Branch>(); s1.Branches.Add(b); s2.Name = "Service 2"; s2.Email = "*****@*****.**"; s2.Description = "servis s2"; s2.Branches = new System.Collections.Generic.List <Branch>(); s2.Branches.Add(b); context.Branches.AddOrUpdate(b); context.Services.AddOrUpdate(s1); context.Services.AddOrUpdate(s2); context.Vehicles.AddOrUpdate(v1); context.Vehicles.AddOrUpdate(v2); context.Types.AddOrUpdate(typeofv); context.SaveChanges(); var userStore = new UserStore <RAIdentityUser>(context); var userManager = new UserManager <RAIdentityUser>(userStore); if (!context.Users.Any(u => u.UserName == "admin")) { var _appUser = context.AppUsers.FirstOrDefault(a => a.FullName == "Admin Adminovic"); var user = new RAIdentityUser() { Id = "admin", UserName = "******", Email = "*****@*****.**", PasswordHash = RAIdentityUser.HashPassword("admin"), AppUserId = _appUser.Id }; userManager.Create(user); userManager.AddToRole(user.Id, "Admin"); } if (!context.Users.Any(u => u.UserName == "appu")) { var _appUser = context.AppUsers.FirstOrDefault(a => a.FullName == "AppUser AppUserovic"); var user = new RAIdentityUser() { Id = "appu", UserName = "******", Email = "*****@*****.**", PasswordHash = RAIdentityUser.HashPassword("appu"), AppUserId = _appUser.Id }; userManager.Create(user); userManager.AddToRole(user.Id, "AppUser"); } Branch b1 = new Branch(); Branch b2 = new Branch(); Service se1 = new Service(); Service se2 = new Service(); Vehicle vh1 = new Vehicle(); Vehicle vh2 = new Vehicle(); // Rent rnt = new Rent(); TypeOfVehicle typeofv1 = new TypeOfVehicle(); TypeOfVehicle typeofv2 = new TypeOfVehicle(); typeofv1.Name = "Caravan"; vh1.Manufactor = "Golf"; vh1.Model = "5"; vh1.PricePerHour = 10; vh1.Unavailable = false; vh1.Year = 2008; vh1.Type = typeofv1; // typeofv.Vehicles = new System.Collections.Generic.List<Vehicle>(); // typeofv.Vehicles.Add(v1); b1.Address = "Ul.1/23"; b1.Latitude = 2222; b1.Longitude = 3333; typeofv2.Name = "Cabriolet"; vh2.Manufactor = "Peugeot"; vh2.Model = "206"; vh2.PricePerHour = 12; vh2.Unavailable = false; vh2.Year = 2015; vh2.Type = typeofv2; // typeofv.Vehicles = new System.Collections.Generic.List<Vehicle>(); // typeofv.Vehicles.Add(v2); b2.Address = "Ul.1/23"; b2.Latitude = 2222; b2.Longitude = 3333; se1.Name = "Service 1"; se1.Email = "*****@*****.**"; se1.Description = "servis s1"; se1.Branches = new System.Collections.Generic.List <Branch>(); se1.Branches.Add(b1); se1.Vehicles = new System.Collections.Generic.List <Vehicle>(); se1.Vehicles.Add(vh1); se2.Name = "Service 2"; se2.Email = "*****@*****.**"; se2.Description = "servis s2"; se2.Branches = new System.Collections.Generic.List <Branch>(); se2.Branches.Add(b2); se2.Vehicles = new System.Collections.Generic.List <Vehicle>(); se2.Vehicles.Add(vh2); Service[] services = { se1, se2 }; context.Services.AddOrUpdate(s => s.Name, services); // context.SaveChanges(); SaveChanges(context); // context.Services.AddOrUpdate(s2); Branch[] branches = { b1, b2 }; context.Branches.AddOrUpdate(bb => bb.Address, branches); // context.SaveChanges(); SaveChanges(context); Vehicle[] vehicles = { vh1, vh2 }; context.Vehicles.AddOrUpdate(v => v.Model, vehicles); //context.SaveChanges(); SaveChanges(context); TypeOfVehicle[] types = { typeofv1, typeofv2 }; context.Types.AddOrUpdate(t => t.Name, types); //context.SaveChanges(); SaveChanges(context); }
public async Task <IHttpActionResult> AddDriver() { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } Driver driver = new Driver(); var httpRequest = HttpContext.Current.Request; try { driver = JsonConvert.DeserializeObject <Driver>(httpRequest.Form[0]); } catch (JsonSerializationException) { return(BadRequest(ModelState)); } var user = new RAIdentityUser() { Id = driver.Username, UserName = driver.Username, Email = driver.Email, AppUser = driver, PasswordHash = RAIdentityUser.HashPassword(driver.PasswordLogin) }; db.AppUsers.Add(driver); IdentityResult result = await UserManager.CreateAsync(user); UserManager.AddToRole(user.Id, driver.Role.ToString()); try { db.SaveChanges(); } catch (DbEntityValidationException) { return(BadRequest(ModelState)); } catch (DbUpdateException) { return(BadRequest(ModelState)); } return(Ok()); }
protected override void Seed(RentApp.Persistance.RADBContext context) { // This method will be called after migrating to the latest version. // You can use the DbSet<T>.AddOrUpdate() helper extension method // to avoid creating duplicate seed data. E.g. // // context.People.AddOrUpdate( // p => p.FullName, // new Person { FullName = "Andrew Peters" }, // new Person { FullName = "Brice Lambson" }, // new Person { FullName = "Rowan Miller" } // ); // if (!context.Roles.Any(r => r.Name == "Admin")) { var store = new RoleStore <IdentityRole>(context); var manager = new RoleManager <IdentityRole>(store); var role = new IdentityRole { Name = "Admin" }; manager.Create(role); } if (!context.Roles.Any(r => r.Name == "Manager")) { var store = new RoleStore <IdentityRole>(context); var manager = new RoleManager <IdentityRole>(store); var role = new IdentityRole { Name = "Manager" }; manager.Create(role); } if (!context.Roles.Any(r => r.Name == "AppUser")) { var store = new RoleStore <IdentityRole>(context); var manager = new RoleManager <IdentityRole>(store); var role = new IdentityRole { Name = "AppUser" }; manager.Create(role); } context.AppUsers.AddOrUpdate( u => u.FullName, new AppUser() { FullName = "Admin Adminovic", Email = "*****@*****.**" } ); context.AppUsers.AddOrUpdate( p => p.FullName, new AppUser() { FullName = "AppUser AppUserovic", Email = "*****@*****.**" } ); //Service ser1 = new Service() { Name = "RentACar", Email = "*****@*****.**" }; TypeOfVehicle type1 = new TypeOfVehicle() { Name = "sport" }; TypeOfVehicle type2 = new TypeOfVehicle() { Name = "jeep" }; Vehicle vehicle1 = new Vehicle() { Model = "A3", Manufactor = "Audi", Year = 2011, PricePerHour = 20, Type = type1 }; Vehicle vehicle2 = new Vehicle() { Model = "Range Rover", Manufactor = "Toyota", Year = 2014, PricePerHour = 30, Type = type2 }; Vehicle vehicle3 = new Vehicle() { Model = "C3", Manufactor = "Renault", Year = 2013, PricePerHour = 10, Type = type1 }; Branch branch1 = new Branch() { Latitude = 46.52, Longitude = 82.65 }; Branch branch2 = new Branch() { Latitude = 32.12, Longitude = 95.16 }; //ser1.Branches = new System.Collections.Generic.List<Branch>(); //ser1.Branches.Add(branch1); //ser1.Branches.Add(branch2); //ser1.Vehicles = new System.Collections.Generic.List<Vehicle>(); //ser1.Vehicles.Add(vehicle1); //ser1.Vehicles.Add(vehicle2); //ser1.Vehicles.Add(vehicle3); //Service ser2 = new Service() { Name = "MiKat", Email = "*****@*****.**" }; Branch branch21 = new Branch() { Latitude = 26.45, Longitude = 65.96 }; Branch branch22 = new Branch() { Latitude = 36.75, Longitude = 68.32 }; //ser2.Branches = new System.Collections.Generic.List<Branch>(); //ser2.Branches.Add(branch21); //ser2.Branches.Add(branch22); //ser2.Vehicles = new System.Collections.Generic.List<Vehicle>(); //ser2.Vehicles.Add(vehicle2); context.Services.AddOrUpdate( s => s.Name, new Service() { Name = "RentACar", Email = "*****@*****.**", Branches = new System.Collections.Generic.List <Branch>() { branch1, branch2 }, Vehicles = new System.Collections.Generic.List <Vehicle>() { vehicle1, vehicle2, vehicle3 } } ); context.Services.AddOrUpdate( m => m.Name, new Service() { Name = "MiKat", Email = "*****@*****.**", Branches = new System.Collections.Generic.List <Branch>() { branch21, branch22 }, Vehicles = new System.Collections.Generic.List <Vehicle>() { vehicle2 } } ); context.SaveChanges(); var userStore = new UserStore <RAIdentityUser>(context); var userManager = new UserManager <RAIdentityUser>(userStore); if (!context.Users.Any(u => u.UserName == "admin")) { var _appUser = context.AppUsers.FirstOrDefault(a => a.FullName == "Admin Adminovic"); var user = new RAIdentityUser() { Id = "*****@*****.**", UserName = "******", Email = "*****@*****.**", PasswordHash = RAIdentityUser.HashPassword("admin"), AppUserId = _appUser.Id }; userManager.Create(user); userManager.AddToRole(user.Id, "Admin"); } if (!context.Users.Any(u => u.UserName == "appu")) { var _appUser = context.AppUsers.FirstOrDefault(a => a.FullName == "AppUser AppUserovic"); var user = new RAIdentityUser() { Id = "*****@*****.**", UserName = "******", Email = "*****@*****.**", PasswordHash = RAIdentityUser.HashPassword("appu"), AppUserId = _appUser.Id }; userManager.Create(user); userManager.AddToRole(user.Id, "AppUser"); } }
public IHttpActionResult Register(RegisterBindingModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } DateTime enteredDate = DateTime.Parse(model.Birth); // //new AppUser() { Username = model.Username, FullName = model.Name + " " + model.Surname } if (db.AppUsers.Find(x => x.Username == model.Username).Count() != 0) { return(BadRequest("Ovaj username vec postoji")); } AppUser appUser = new AppUser { Approved = false, BirthDate = enteredDate, CreateService = model.CreateService, Surname = model.Surname, LoggedIn = false, Name = model.Name, Username = model.Username, Contact = model.Contact }; var userStore = new UserStore <RAIdentityUser>(new RADBContext()); var userManager = new UserManager <RAIdentityUser>(userStore); var user = new RAIdentityUser() { UserName = model.Username, Email = model.Email, Id = model.Username, AppUser = appUser, PasswordHash = RAIdentityUser.HashPassword(model.Password) }; userManager.Create(user); if (model.CreateService) { userManager.AddToRole(user.Id, "Manager"); } else { userManager.AddToRole(user.Id, "AppUser"); } Hubs.NotificationHub.SendNotification("New client created account. To view his document, go to admin panel."); return(Ok()); }