Exemplo n.º 1
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(
            IApplicationBuilder app,
            IWebHostEnvironment env,
            IAppUserService appUserService,
            IAppUserRoleService appUserRoleService,
            IAppRoleService appRoleService
            )
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseExceptionHandler("/Error");

            JwtIdentityInitializer.Seed(appUserService, appUserRoleService, appRoleService).Wait();

            app.UseRouting();
            app.UseAuthentication();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
Exemplo n.º 2
0
        public async Task <IActionResult> SignUp(AppUserAddDto appUserAddDto, [FromServices] IAppRoleService appRoleService,
                                                 [FromServices] IAppUserRoleService appUserRoleService)
        {
            var appUser = await _appUserService.FindByUserNameAsync(appUserAddDto.UserName);

            if (appUser != null)
            {
                return(BadRequest($"{appUserAddDto.UserName} already exists"));
            }

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

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

            var userRole = await appRoleService.FindByName(RoleInfo.Member);

            await appUserRoleService.InsertAsync(new AppUserRole
            {
                AppRoleId = userRole.Id,
                AppUserId = createdUser.Id
            });


            return(Created("", appUserAddDto));
        }
Exemplo n.º 3
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env,
                              IAppUserService appUserService, IAppUserRoleService appUserRoleService
                              , IAppRoleService appRoleService)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            //addSwagger
            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
            });

            app.UseExceptionHandler("/Error"); //Bir hata olunca buraya git. localhost/error

            JwtIdentityInitializer.Seed(appUserService, appUserRoleService, appRoleService).Wait();

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthentication(); //token için authorization üstünde olmalý

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
Exemplo n.º 4
0
        public async Task <IActionResult> SignUp(AppUserAddDto appUserAddDto, [FromServices] IAppUserRoleService appUserRoleService, [FromServices] IAppRoleService appRoleService)
        {
            var appUser = await _appUserService.FindByUserName(appUserAddDto.FullName);

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

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

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

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


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


            return(Created("", appUserAddDto));
        }
Exemplo n.º 5
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 }));
        }
Exemplo n.º 6
0
 public AuthController(IAppUserService appUserService, IAppRoleService appRoleService, IAppUserRoleService appUserRoleService, IJwtService jwtService, IWebHostEnvironment hostingEnvironment)
 {
     _appUserService = appUserService;
     _appRoleService = appRoleService;
     _appUserRoleService = appUserRoleService;
     _jwtService = jwtService;
     _hostingEnvironment = hostingEnvironment;
 }
Exemplo n.º 7
0
 public AppUserService(IRepositoryContext context, IAppUserRepository appUserRepository
                       , IAppRoleRepository appRoleRepository
                       , IAppUserRoleService appUserRoleService) : base(context)
 {
     this.appUserRepository  = appUserRepository;
     this.appRoleRepository  = appRoleRepository;
     this.appUserRoleService = appUserRoleService;
 }
        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
                });
            }
        }
Exemplo n.º 9
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
                });
            }
        }
Exemplo n.º 10
0
        public static async Task Seed(IAppUserService appUserService, IAppUserRoleService appUserRoleService,
                                      IAppRoleService appRoleService)
        {
            var adminRole = await appRoleService.FindByName(RoleInfo.Admin);

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



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

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

            var adminUser = await appUserService.FindByUserName("veysel");

            if (adminUser == null)
            {
                await appUserService.Add(new AppUser()
                {
                    FullName = "ahmet veysel bozoğlu",
                    Password = "******",
                    UserName = "******"
                });


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

                var admin = await appUserService.FindByUserName("veysel");

                await appUserRoleService.Add(new AppUserRole
                {
                    AppUserId = admin.Id,
                    AppRoleId = role.Id
                });
            }
        }
Exemplo n.º 11
0
        public static async Task Seed(IAppUserService appUserService, IAppUserRoleService appUserRoleService, IAppRoleService appRoleService)
        {
            //ilgili rol varmı?
            var adminRole = await appRoleService.FindByName(RoleInfo.Admin);

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

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

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

            var adminUser = await appUserService.FindByUserName("sametirkoren");

            if (adminUser == null)
            {
                await appUserService.Add(new AppUser
                {
                    FullName = "Samet İrkören",
                    UserName = "******",
                    Password = "******"
                });

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

                var admin = await appUserService.FindByUserName("sametirkoren");

                await appUserRoleService.Add(new AppUserRole
                {
                    AppUserId = admin.Id,
                    AppRoleId = role.Id
                });
            }
        }
        public async static Task Seed(IAppUserService appUserService, IAppUserRoleService appUserRoleService,
                                      IAppRoleService appRoleService)
        {
            var adminRole = await appRoleService.FindByName("Admin");

            var memberRole = await appRoleService.FindByName("Member");

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

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

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

            if (adminUser == null)
            {
                await appUserService.InsertAsync(new AppUser
                {
                    FullName = "Erol Aksoy",
                    Password = "******",
                    UserName = "******"
                });

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

                var role = await appRoleService.FindByName("Admin");

                await appUserRoleService.InsertAsync(new AppUserRole
                {
                    AppRoleId = role.Id,
                    AppUserId = admin.Id
                });
            }
        }
Exemplo n.º 13
0
        public static async Task Seed(IAppUserService appUserService, IAppUserRoleService appUserRoleService, IAppRoleService appRoleService)
        {
            var adminRole = await appRoleService.FindByName(RoleInfo.Admin);

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

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

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

            var adminUser = await appUserService.FindByUserName("savas.ev");

            if (adminUser == null)
            {
                await appUserService.Add(new AppUser
                {
                    FullName = "Savaş Ev",
                    UserName = "******",
                    Password = "******"
                });

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

                var user = await appUserService.FindByUserName("savas.ev");

                await appUserRoleService.Add(new AppUserRole
                {
                    AppUserId = user.Id,
                    AppRoleId = role.Id
                });
            }
        }
Exemplo n.º 14
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
                });
            }
        }
Exemplo n.º 15
0
        public static async Task Seed(IAppUserService appUserService, IAppUserRoleService appUserRoleService, IAppRoleService appRoleService)
        {
            var adminRole = await appRoleService.FindByName(RoleInfo.Admin);

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

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

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

            var adminnUser = await appUserService.FindByUserName("sercan");

            if (adminnUser == null)
            {
                await appUserService.Add(new AppUser
                {
                    FullName = "sercan fırtına",
                    UserName = "******",
                    Password = "******"
                });

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

                var admin = await appUserService.FindByUserName("sercan");

                await appUserRoleService.Add(new AppUserRole
                {
                    AppUserId = admin.Id,
                    AppRoleId = role.Id
                });
            }
        }
Exemplo n.º 16
0
        public static async Task Seed(IAppUserService appUserService,
                                      IAppUserRoleService appUserRoleService, IAppRoleService appRoleService)
        {
            var adminRole = await appRoleService.FindByName(RoleInfo.Admin);

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

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

            if (adminRole == null)
            {
                await appRoleService.AddAsync(new Entities.Concrete.AppRole
                {
                    Name = RoleInfo.Member
                });
            }
            var adminUser = await appUserService.FindByUserName("Uzay");

            if (adminUser == null)
            {
                await appUserService.AddAsync(new AppUser
                {
                    FullName = "Uzay KAHRAMAN",
                    UserName = "******",
                    Password = "******"
                });

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

                var admin = await appUserService.FindByUserName("Uzay");

                await appUserRoleService.AddAsync(new AppUserRole
                {
                    AppUserId = admin.Id,
                    AppRoleId = role.Id
                });
            }
        }
Exemplo n.º 17
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));
        }
Exemplo n.º 18
0
        public static async Task Seed(IAppUserService appUserService, IAppRoleService appRoleService, IAppUserRoleService appUserRoleService)
        {
            //have a role
            var adminRole = await appRoleService.FindByName(RoleInfo.Admin);

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

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

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

            var adminUser = await appUserService.FindByUserName("enesb");

            if (adminUser == null)
            {
                await appUserService.Add(new AppUser
                {
                    UserName = "******",
                    FullName = "enes baysal",
                    Password = "******"
                });

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

                var admin = await appUserService.FindByUserName("enesb");

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