Пример #1
0
        public async Task <ResetPasswordResponse> Handle(ResetPasswordRequest request, CancellationToken cancellationToken)
        {
            var result = new ResetPasswordResponse();

            var user = await _context.Users.FindAsync(request.UserID);

            if (user != null)
            {
                string passwordSalt;
                string passwordHash;

                if (!string.IsNullOrEmpty(request.Password))
                {
                    if (!AppUtility.VerifyPasswordHash(request.Password, user.PasswordSalt, user.PasswordHash))
                    {
                        passwordSalt = AppUtility.CreatePasswordSalt();
                        passwordHash = AppUtility.CreatePasswordHash(request.Password, passwordSalt);

                        user.PasswordHash = passwordHash;
                        user.PasswordSalt = passwordSalt;
                    }
                }

                await _context.SaveChangesAsync(cancellationToken);
            }
            else
            {
                throw new Exception("Maaf, User tidak ditemukan");
            }

            return(result);
        }
Пример #2
0
        public async Task <UpdateUserResponse> Handle(UpdateUserRequest request, CancellationToken cancellationToken)
        {
            var result = new UpdateUserResponse();

            var user = await _context.Users.FindAsync(request.UserID);

            if (user != null)
            {
                string passwordSalt;
                string passwordHash;

                if (!string.IsNullOrEmpty(request.Password))
                {
                    if (!AppUtility.VerifyPasswordHash(request.Password, user.PasswordSalt, user.PasswordHash))
                    {
                        passwordSalt = AppUtility.CreatePasswordSalt();
                        passwordHash = AppUtility.CreatePasswordHash(request.Password, passwordSalt);

                        user.PasswordHash = passwordHash;
                        user.PasswordSalt = passwordSalt;
                    }
                }

                if (user.Username != request.Username || user.Email != request.Email)
                {
                    if (await _context.Users.AsNoTracking().AnyAsync(x => x.Username == request.Username || x.Email == request.Email, cancellationToken))
                    {
                        throw new Exception("Maaf Username atau Email sudah di gunakan");
                    }
                    else
                    {
                        user.Username = request.Username;
                        user.Email    = request.Email;
                    }
                }

                user.Alamat      = request.Alamat;
                user.Fullname    = request.Fullname;
                user.Instansi    = request.Instansi;
                user.Jabatan     = request.Jabatan;
                user.NIP         = request.NIP;
                user.Telepon     = request.Telepon;
                user.RoleID      = request.RoleID;
                user.ProvinsiID  = request.ProvinsiID;
                user.KotaID      = request.KotaID;
                user.KecamatanID = request.KecamatanID;
                user.KelurahanID = request.KelurahanID;

                await _context.SaveChangesAsync(cancellationToken);
            }
            else
            {
                throw new Exception("Maaf, User tidak ditemukan");
            }

            return(result);
        }
Пример #3
0
        public async Task <CreateUserResponse> Handle(CreateUserRequest request, CancellationToken cancellationToken)
        {
            var result = new CreateUserResponse();

            if (!string.IsNullOrEmpty(request.Username))
            {
                if (await _context.Users.AnyAsync(x => x.Username == request.Username))
                {
                    throw new Exception("Maaf Username sudah di gunakan");
                }
            }

            if (!string.IsNullOrEmpty(request.Email))
            {
                if (await _context.Users.AnyAsync(x => x.Email == request.Email, cancellationToken))
                {
                    throw new Exception("Maaf Email sudah di gunakan");
                }
            }
            string passwordSalt = AppUtility.CreatePasswordSalt();
            string passwordHash = AppUtility.CreatePasswordHash(request.Password, passwordSalt);

            var user = new User
            {
                Username     = request.Username,
                PasswordHash = passwordHash,
                PasswordSalt = passwordSalt,

                Alamat   = request.Alamat,
                Email    = request.Email,
                Fullname = request.Fullname,
                Instansi = request.Instansi,
                Jabatan  = request.Jabatan,
                NIP      = request.NIP,
                Telepon  = request.Telepon,

                RoleID      = request.RoleID,
                ProvinsiID  = request.ProvinsiID,
                KotaID      = request.KotaID,
                KecamatanID = request.KecamatanID,
                KelurahanID = request.KelurahanID
            };

            await _context.Users.AddAsync(user);

            await _context.SaveChangesAsync(cancellationToken);

            result.UserID   = user.UserID;
            result.Username = user.Username;

            return(result);
        }
Пример #4
0
        public async Task <SeedUserResponse> Handle(SeedUserRequest request, CancellationToken cancellationToken)
        {
            var result = new SeedUserResponse();

            List <Kelurahan> Kelurahans = await _context.Kelurahans.ToListAsync(cancellationToken);

            var listData = await _entityRepository.GetAllUser();

            var listkelurahanJSON = await _entityRepository.GetAllKelurahan();

            //var listRoleJSON = await _entityRepository.GetAllKelurahan(); // get all users

            // this is temporary just to make sure there is no duplicate data
            List <User> users = new List <User>();

            // collect data from db to temporary List
            var provinsi = await _context.Provinsis.AsNoTracking().ToListAsync(cancellationToken);

            var kota = await _context.Kotas.AsNoTracking().ToListAsync(cancellationToken);

            var kecamatan = await _context.Kecamatans.AsNoTracking().ToListAsync(cancellationToken);

            var kelurahan = await _context.Kelurahans.AsNoTracking().ToListAsync(cancellationToken);

            var role = await _context.Roles.AsNoTracking().ToListAsync(cancellationToken);

            var existingDatas = await _context.Users.AsNoTracking().ToListAsync(cancellationToken);

            if (listData.Count() > 0)
            {
                foreach (var data in listData)
                {
                    if (existingDatas.Any(x => x.id == data.id))
                    {
                        continue;
                    }

                    User user = new User();

                    user = users
                           .SingleOrDefault(x => x.id == data.id);

                    if (user == null)
                    {
                        var originRoleName = "";

                        if (data.level == "admin")
                        {
                            originRoleName = "ADMIN";
                        }
                        else if (data.level == "user")
                        {
                            originRoleName = "USER_PROVINSI";
                        }
                        else if (data.level == "usaha")
                        {
                            originRoleName = "USER_USAHA";
                        }
                        else if (data.level == "user_kab")
                        {
                            originRoleName = "USER_KABUPATEN";
                        }

                        var getRoleID = role.Where(x => x.Name.Contains(originRoleName)).FirstOrDefault();
                        System.Console.WriteLine($"getRoleID: {getRoleID?.RoleID} - {getRoleID?.Name}");

                        var getProvinsiID  = provinsi.Where(x => x.id_provinsi == data.provinsi).FirstOrDefault();
                        var getKotaID      = kota.Where(x => x.id_kota == data.kota).FirstOrDefault();
                        var getKecamatanID = kecamatan.Where(x => x.id_kecamatan == data.kecamatan).FirstOrDefault();

                        var id_kelurahan   = listkelurahanJSON.Where(x => x.nama_kelurahan == data.desa).FirstOrDefault();
                        var getKelurahanID = kelurahan.Where(x => x.id_kelurahan == (id_kelurahan != null ? id_kelurahan.id_kelurahan : "")).FirstOrDefault();

                        string passwordSalt = AppUtility.CreatePasswordSalt();
                        string passwordHash = AppUtility.CreatePasswordHash(data.pass, passwordSalt);

                        System.Console.WriteLine($"INSERT - User");

                        user = new User
                        {
                            id = data.id,

                            Username = data.username,
                            Fullname = data.nama,
                            Email    = data.email,
                            Alamat   = data.alamat,
                            NIP      = data.nip,
                            Jabatan  = data.jabatan,
                            Instansi = data.instansi,
                            Telepon  = data.telpon,

                            PasswordHash = passwordHash,
                            PasswordSalt = passwordSalt,

                            RoleID      = getRoleID != null ? getRoleID.RoleID : null,
                            ProvinsiID  = getProvinsiID != null ? getProvinsiID.ProvinsiID : null,
                            KotaID      = getKotaID != null ? getKotaID.KotaID : null,
                            KecamatanID = getKecamatanID != null ? getKecamatanID.KecamatanID : null,
                            KelurahanID = getKelurahanID != null ? getKelurahanID.KelurahanID : null
                        };

                        users.Add(user);

                        _context.Users.Add(user);
                    }
                }
            }

            await _context.SaveChangesAsync(cancellationToken);

            result.IsSuccessful = true;

            return(result);
        }