public static async Task Seed(IAppUserService appUserService, IAppUserRoleService appUserRoleService
                                      , IAppRoleService appRoleService)
        {
            //Rol varsa eklemicem yoksa eklicem
            var adminRole = await appRoleService.FindByNameAsync(RoleInfo.Admin);

            if (adminRole == null)
            {
                await appRoleService.Add(new AppRole
                {
                    Name = RoleInfo.Admin
                });
            }
            var memberRole = await appRoleService.FindByNameAsync(RoleInfo.Member);

            if (memberRole == null)
            {
                await appRoleService.Add(new AppRole
                {
                    Name = RoleInfo.Member
                });
            }

            //admin kullanıcı eklmedim.
            var adminUser = await appUserService.FindByUserNameAsync("ADMIN");

            if (adminUser == null)
            {
                await appUserService.Add(new AppUser
                {
                    UserName = "******",
                    Password = "******"
                });
            }
            //appuserrole tablosuna bu ilişkiyi eklemem lazım
            var role = await appRoleService.FindByNameAsync(RoleInfo.Admin);

            var admin = await appUserService.FindByUserNameAsync("ADMIN");

            //daha önce eklenmiş mi
            var allUSerRole = await appUserRoleService.GetAll();

            int kontrol = allUSerRole.Where(x => x.AppRoleId == role.Id && x.AppUserId == admin.Id).Count();

            if (kontrol == 0)
            {
                await appUserRoleService.Add(new AppUserRole
                {
                    AppRoleId = role.Id,
                    AppUserId = admin.Id
                });
            }
        }
Пример #2
0
        public static async Task Seed(IAppUserService appUserService, IAppUserRoleService appUserRoleService, IAppRoleService appRoleService)
        {
            var adminRole = await appRoleService.FindByNameAsync(RoleInfo.Admin);

            if (adminRole == null)
            {
                await appRoleService.AddAsync(new Entities.Concrete.AppRole
                {
                    Name = RoleInfo.Admin
                });
            }

            var memberRole = await appRoleService.FindByNameAsync(RoleInfo.Member);

            if (memberRole == null)
            {
                await appRoleService.AddAsync(new Entities.Concrete.AppRole
                {
                    Name = RoleInfo.Member
                });
            }

            var adminUser = await appUserService.FindByEmail("*****@*****.**");

            if (adminUser == null)
            {
                string password = BCrypt.Net.BCrypt.HashPassword("1");

                await appUserService.AddAsync(new Entities.Concrete.AppUser
                {
                    Name      = "Admin Test",
                    Password  = password,
                    Email     = "*****@*****.**",
                    ImagePath = "default.jpg",
                    UserName  = "******",
                    Surname   = "Admin Surname",
                });


                var role = await appRoleService.FindByNameAsync(RoleInfo.Admin);

                var admin = await appUserService.FindByEmail("*****@*****.**");

                await appUserRoleService.AddAsync(new Entities.Concrete.AppUserRole
                {
                    AppUserId = admin.Id,
                    AppRoleId = role.Id
                });
            }
        }
Пример #3
0
        public async Task <IActionResult> SignUp(AppUserAddDto appUserAddDto, [FromServices] IAppUserRoleService appUserRoleService, [FromServices] IAppRoleService appRoleService)
        {
            appUserAddDto.Email    = appUserAddDto.Email.ToLower();
            appUserAddDto.UserName = appUserAddDto.UserName.ToLower();
            var appUser = await _appUserService.FindByEmail(appUserAddDto.Email);

            if (appUser != null)
            {
                return(BadRequest(new { Error = $"{appUserAddDto.Email} alınmıştır. Farklı bir e-posta dene", ErrorType = "EMAIL_IS_ALREADY_IN_USE" }));
            }

            appUserAddDto.Password  = BCrypt.Net.BCrypt.HashPassword(appUserAddDto.Password);
            appUserAddDto.ImagePath = "default.jpg";

            await _appUserService.AddAsync(_mapper.Map <AppUser>(appUserAddDto));

            var user = await _appUserService.FindByEmail(appUserAddDto.Email);

            var role = await appRoleService.FindByNameAsync(RoleInfo.Member);

            await appUserRoleService.AddAsync(new AppUserRole
            {
                AppRoleId = role.Id,
                AppUserId = user.Id
            });

            var newUser = await _appUserService.FindByEmail(appUserAddDto.Email);

            var roles = await _appUserService.GetRolesByEmail(appUserAddDto.Email);

            var token = _jwtService.GenerateJwt(newUser, roles);

            return(Created("", new { token.Token, newUser.Id, expiresIn = 30 * 60 }));
        }
Пример #4
0
        public static async Task Seed(IAppUserService appUserService, IAppUserRoleService appUserRoleService, IAppRoleService appRoleService)
        {
            var adminRole = await appRoleService.FindByNameAsync(RoleInfo.Admin);

            if (adminRole == null)
            {
                await appRoleService.AddAsync(new AppRole
                {
                    Name = RoleInfo.Admin
                });
            }

            var memberRole = await appRoleService.FindByNameAsync(RoleInfo.Member);

            if (memberRole == null)
            {
                await appRoleService.AddAsync(new AppRole
                {
                    Name = RoleInfo.Member
                });
            }

            var adminUser = await appUserService.FindByUserNameAsync("emre");

            if (adminUser == null)
            {
                await appUserService.AddAsync(new AppUser
                {
                    FullName = "emre yüksek",
                    UserName = "******",
                    Password = "******"
                });

                var role = await appRoleService.FindByNameAsync(RoleInfo.Admin);

                var admin = await appUserService.FindByUserNameAsync("emre");

                await appUserRoleService.AddAsync(new AppUserRole
                {
                    AppUserId = admin.Id,
                    AppRoleId = role.Id
                });
            }
        }
Пример #5
0
        public async Task <IActionResult> SignUp(AppUserAddDto appUserAddDto, [FromServices] IAppUserRoleService appUserRoleService, [FromServices] IAppRoleService appRoleService)
        {
            var appUser = await _appUserService.FindByUserNameAsync(appUserAddDto.UserName);

            if (appUser != null)
            {
                return(BadRequest($"{appUserAddDto.UserName} zaten alınmış"));
            }

            await _appUserService.AddAsync(_mapper.Map <AppUser>(appUserAddDto));

            var user = await _appUserService.FindByUserNameAsync(appUserAddDto.UserName);

            var role = await appRoleService.FindByNameAsync(RoleInfo.Member);

            await appUserRoleService.AddAsync(new AppUserRole
            {
                AppRoleId = role.Id,
                AppUserId = user.Id
            });

            return(Created("", appUserAddDto));
        }
Пример #6
0
        public async Task <IActionResult> Register([FromQuery] AppUserAddDto appUserAddDto,
                                                   [FromServices] IAppUserRoleService appUserRoleService,
                                                   [FromServices] IAppRoleService appRoleService)
        {
            var user = await this.appUserService.FindByUserNameAsync(appUserAddDto.Name);

            if (user != null)
            {
                return(BadRequest($"{appUserAddDto.UserName} is already registered in the system"));
            }
            await this.appUserService.Add(this.mapper.Map <AppUser>(appUserAddDto));

            var appuser = await this.appUserService.FindByUserNameAsync(appUserAddDto.Name);

            var role = await appRoleService.FindByNameAsync(RoleInfo.Member);

            await appUserRoleService.Add(new AppUserRole
            {
                AppUserId = appuser.Id,
                AppRoleId = role.Id
            });

            return(Created("", appUserAddDto));
        }