Beispiel #1
0
        private async Task <IdentityResult> EnsureRole(
            ICustomRoleManager roleManager,
            ICustomUserManager userManager,
            string uid, string role)
        {
            IdentityResult IR = null;

            if (roleManager == null)
            {
                throw new Exception("roleManager null");
            }

            if (!await roleManager.RoleExistsAsync(role))//ensure that current role doesn't exists and create it
            {
                IR = await roleManager.CreateAsync(new CustomRole(role));
            }

            var user = await userManager.FindByIdAsync(uid);

            if (user == null)
            {
                throw new Exception("User does not exists!");
            }

            if (!await userManager.IsInRoleAsync(user, role))
            {
                IR = await userManager.AddToRoleAsync(user, role);
            }


            return(IR);
        }
 public PermissionManager(ICustomRoleManager <TRole> roleManager, ICustomUserManager <TUser> userManager, IHttpContextAccessor httpContextAccessor, ILogger <PermissionManager <TUser, TRole> > logger)
 {
     _roleManager         = roleManager;
     _userManager         = userManager;
     _httpContextAccessor = httpContextAccessor;
     _logger = logger;
 }
Beispiel #3
0
 public CommentController(
     ICustomAuthorizationService authorizationService,
     ICustomUserManager userManager,
     ILogger <DI_BaseController> logger,
     ICommentsRepository repository) : base(authorizationService, userManager, logger)
 {
     _repository = repository;
 }
 public AuthenticationService(ICustomUserManager customUserManager, ICustomRoleManager customRoleManager,
                              IPasswordHasher <User> passwordHasher, IClientRepository clientRepository)
 {
     _customUserManager = customUserManager;
     _customRoleManager = customRoleManager;
     _passwordHasher    = passwordHasher;
     _clientRepository  = clientRepository;
 }
Beispiel #5
0
 public DI_BaseController(
     ICustomAuthorizationService authorizationService,
     ICustomUserManager userManager,
     ILogger <DI_BaseController> logger) : base()
 {
     _userManager          = userManager;
     _authorizationService = authorizationService;
     _logger = logger;
 }
Beispiel #6
0
 public CustomAccountController(
     ICustomUserManager userManager,
     ICustomSignInManager signInManager,
     ILogger <CustomAccountController> logger,
     IEmailSender emailSender)
 {
     _userManager   = userManager;
     _signInManager = signInManager;
     _logger        = logger;
     _emailSender   = emailSender;
 }
Beispiel #7
0
        private async Task <string> EnsureUser(
            ICustomUserManager userManager,
            CustomUser newUser,
            string Password)
        {
            var user = await userManager.FindByNameAsync(newUser.UserName);

            if (user == null)
            {
                user = newUser;
                userManager.CreateAsync(user, Password).Wait();
            }
            return(user.Id);
        }
        //private readonly IFacebookAuthService _facebookAuthService;

        public IdentityService(
            ICustomUserManager userManager,
            JwtSettings jwtSettings,
            TokenValidationParameters tokenValidationParameters,
            VehicleDbContext context,
            //RoleManager<IdentityRole> roleManager
            ICustomRoleManager roleManager
            //IFacebookAuthService facebookAuthService
            )
        {
            _userManager = userManager;
            _jwtSettings = jwtSettings;
            _tokenValidationParameters = tokenValidationParameters;
            _context     = context;
            _roleManager = roleManager;
            //_facebookAuthService = facebookAuthService;
        }
 public NoticetIsOwnerAuthorizationHandler(ICustomUserManager
                                           userManager)
 {
     _userManager = userManager;
 }
Beispiel #10
0
        //public List<string> userIds = new List<string>();


        public async Task Initialize(
            ICustomUserManager userManager,
            ICustomRoleManager roleManager,
            VehicleDbContext context)
        {
            context.Database.Migrate();


            if (!context.Cars.Any())
            {
                for (int i = 0; i < Cars.Length; i++)
                {
                    var ImgPath = _vehicleImageRetriever
                                  .GetFilePathByVehicleBrandAndUniqueNumber
                                      (Cars[i].Brand, Cars[i].UniqueNumber, _imgDirectory);
                    Cars[i].ImgPath = ImgPath;
                }
                context.Cars.AddRange(Cars);
                context.SaveChanges();
            }
            if (!context.Penalties.Any())
            {
                var rnd = new Random();

                var carsFromDb = await context.Cars.AsNoTracking().ToListAsync();

                var finalPenalties = new List <Penalty>();
                for (int i = 0; i < carsFromDb.Count; i++)
                {
                    var index = rnd.Next(0, carsFromDb.Count - 1);
                    var car   = carsFromDb[index];
                    var countOfPenaltiesForCar = rnd.Next(0, 5);
                    for (int j = 0; j < countOfPenaltiesForCar; j++)
                    {
                        var tempPenalties = new List <Penalty>(Penalties);
                        var penaltyIndex  = rnd.Next(0, tempPenalties.Count - 1);
                        var penalty       = tempPenalties[penaltyIndex];
                        penalty.CarId = car.Id;
                        finalPenalties.Add(penalty);

                        tempPenalties.Remove(penalty);
                    }

                    carsFromDb.Remove(car);
                }
                context.Penalties.AddRange(finalPenalties);
                context.SaveChanges();
            }

            //using(userManager){

            if (!userManager.Users.AsNoTracking().Any())
            {
                //add password
                //new thread errors
                foreach (var user_password in UsersAndPasswords.ToList())
                {
                    var id = EnsureUser(userManager,
                                        user_password.CustomUser,
                                        user_password.Password).GetAwaiter().GetResult();
                }

                context.SaveChanges();
            }

            //TODO: get admins data from ignored file
            var adminFirstName = "admin_jHHHg3nnnwDn";
            var adminPassword  = "******";
            var adminEmail     = "*****@*****.**";
            var adminUserName  = "******";

            var admin = await userManager.Users
                        .FirstOrDefaultAsync(u => u.FirstName == adminFirstName);

            if (admin == null)
            {
                var adminId = await this.EnsureUser(
                    userManager,
                    new CustomUser()
                {
                    Email          = adminEmail,
                    UserName       = adminUserName,
                    FirstName      = adminFirstName,
                    EmailConfirmed = true,
                    BirthDate      = DateTime.Parse("1978-02-06"),
                },
                    adminPassword
                    );

                await this.EnsureRole(
                    roleManager,
                    userManager,
                    adminId,
                    AuthorizationConstants.ContactAdministratorsRole);

                context.SaveChanges();
            }
            //}

            //exeption here works from second start because for the first time users are not initialized due to userManager
            //https://entityframework.net/knowledge-base/7819002/the-insert-statement-conflicted-with-the-foreign-key-constraint-in-entity-framework

            if (!context.ManyToManyCarOwners.Any())
            {
                try
                {
                    await TryAddManyToManyRelation(context);
                }
                catch (Exception ex)
                {
                    Console.ForegroundColor = ConsoleColor.Blue;
                    Console.WriteLine(ex);
                }
            }
        }
Beispiel #11
0
 public Authenticator(IAuthenticationService authenticationService, ICustomRoleManager customRoleManager, ICustomUserManager customUserManager)
 {
     _authenticationService = authenticationService;
     _customRoleManager     = customRoleManager;
     _customUserManager     = customUserManager;
 }
 public AccountController(ICustomUserManager userManager)
 {
     _userManager = userManager;
     _dbContext   = new DoorsDatabaseContext();
 }