// public DbSet<FormFile> Images { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { /*modelBuilder.Entity<Location>(entity => * { * entity.HasKey(e => e.Id); * });*/ //data seed //superadmin modelBuilder.Entity <User>().HasData(new User() { Id = -2, Email = "admin", PasswordHash = CryptographyProcessor.Hash("admin"), UserRole = UserRoles.SuperAdmin }); modelBuilder.Entity <Field>().HasIndex(field => field.ParentType); // modelBuilder.Entity<Field>().HasOne<FieldValue>().WithMany(value => value.FieldId) modelBuilder.Entity <Field>() .Property(c => c.ParentType) .HasConversion <int>(); base.OnModelCreating(modelBuilder); }
public async Task <IActionResult> CreateAccount(string email, string pass, UserRoles userRole) { if (_dbContext.Users.Any(user => user.Email.ToLower() == email.ToLower())) { return(BadRequest("User with such email already exists")); } //if requested creation of admins then checking rights if (userRole == UserRoles.Admin) { if (User.FindFirst("AccessLevel")?.Value != UserRoles.SuperAdmin.ToString()) { return(Unauthorized("you have to be SuperAdmin to create other admins")); } } if (userRole == UserRoles.SiteAdmin) { if (User.FindFirst("AccessLevel")?.Value != UserRoles.Admin.ToString() && User.FindFirst("AccessLevel")?.Value != UserRoles.SuperAdmin.ToString()) { return(Unauthorized("you have to be Admin or superAdmin to create site admins")); } } //generate pass //todo: in prod make pass stronger // var pass = Guid.NewGuid().ToString("n").Substring(0, 3); var computedHash = CryptographyProcessor.Hash(pass); //send email // var res = await _mailService.SendRegistrationMail(email, pass); var usr = new User() { Email = email, PasswordHash = computedHash, UserRole = userRole }; _dbContext.Users.Add(usr); _dbContext.SaveChanges(); return(Ok(usr)); }
public async Task <IActionResult> GetHash(string password) { return(Ok(CryptographyProcessor.Hash(password))); }