Пример #1
0
        //  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);
        }
Пример #2
0
        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));
        }
Пример #3
0
 public async Task <IActionResult> GetHash(string password)
 {
     return(Ok(CryptographyProcessor.Hash(password)));
 }