public Model.Korisnici Insert(KorisniciUpsertRequest request)
        {
            var entitet = _mapper.Map <Database.Korisnici>(request);

            if (request.Lozinka != request.PotvrdiLozinku)
            {
                throw new UserException("Lozinke se ne podudaraju");
            }

            entitet.LozinkaSalt = Util.PasswordGenerator.GenerateSalt();
            entitet.LozinkaHash = Util.PasswordGenerator.GenerateHash(request.Lozinka, entitet.LozinkaSalt);

            _context.Korisnici.Add(entitet);

            _context.SaveChanges();

            foreach (var item in request.Uloge)
            {
                var korisniciUloga = new Database.KorisniciUloge();
                korisniciUloga.DatumIzmjene = DateTime.Now;
                korisniciUloga.KorisnikId   = entitet.Id;
                korisniciUloga.UlogaId      = item;
                _context.KorisniciUloge.Add(korisniciUloga);
            }

            _context.SaveChanges();

            return(_mapper.Map <Model.Korisnici>(entitet));
        }
        public Model.Korisnici Insert(KorisniciInsertRequest request)
        {
            var entity = _mapper.Map <Database.Korisnici>(request);

            if (request.Password != request.PasswordConfirmation)
            {
                throw new Exception("Passwordi se ne slažu");
            }

            entity.LozinkaSalt = GenerateSalt();
            entity.LozinkaHash = GenerateHash(entity.LozinkaSalt, request.Password);

            _context.Korisnici.Add(entity);
            _context.SaveChanges();

            foreach (var uloga in request.Uloge)
            {
                Database.KorisniciUloge korisniciUloge = new Database.KorisniciUloge();
                korisniciUloge.KorisnikID   = entity.KorisnikID;
                korisniciUloge.UlogaID      = uloga;
                korisniciUloge.DatumIzmjene = DateTime.Now;
                _context.KorisniciUloge.Add(korisniciUloge);
            }
            _context.SaveChanges();

            //infinit loop fix
            foreach (var item in entity.KorisniciUloge)
            {
                item.Korisnik = null;
            }

            return(_mapper.Map <Model.Korisnici>(entity));
        }
Exemplo n.º 3
0
        public OnlineGym_Model.Korisnik Insert(KorisniciInsertRequest request)
        {
            var entity = _mapper.Map <Database.Korisnik>(request);

            if (request.Password != request.PasswordPotvrda)
            {
                throw new Exception("Passwordi se ne slažu");
            }

            entity.LozinkaSalt = GenerateSalt();
            entity.LozinkaHash = GenerateHash(entity.LozinkaSalt, request.Password);

            _context.Korisnik.Add(entity);
            _context.SaveChanges();

            foreach (var uloga in request.Uloge)
            {
                Database.KorisniciUloge korisniciUloge = new Database.KorisniciUloge();
                korisniciUloge.KorisnikId = entity.KorisnikId;
                korisniciUloge.UlogaId    = uloga;
                _context.KorisniciUloge.Add(korisniciUloge);
            }
            _context.SaveChanges();

            return(_mapper.Map <OnlineGym_Model.Korisnik>(entity));
        }
Exemplo n.º 4
0
        public Model.KorisniciModel Insert(KorisniciInsertRequest request)
        {
            var entity = _mapper.Map <Korisnici>(request);

            if (request.Password != request.PasswordPotvrda)
            {
                throw new Exception("Passwordi se ne slažu");
            }

            entity.LozinkaSalt = GenerateSalt();
            entity.LozinkaHash = GenerateHash(entity.LozinkaSalt, request.Password);

            _context.Korisnici.Add(entity);
            _context.SaveChanges();

            var uloge = request.Uloge;

            if (uloge == null || uloge.Count < 1)
            {
                uloge.Add(_context.Uloge.Where(x => x.Naziv == "korisnik").FirstOrDefault().UlogaId);
            }
            foreach (var uloga in uloge)
            {
                Database.KorisniciUloge korisniciUloge = new Database.KorisniciUloge();
                korisniciUloge.KorisnikId   = entity.KorisnikId;
                korisniciUloge.UlogaId      = uloga;
                korisniciUloge.DatumIzmjene = DateTime.Now;
                _context.KorisniciUloge.Add(korisniciUloge);
            }
            _context.SaveChanges();

            return(_mapper.Map <Model.KorisniciModel>(entity));
        }
Exemplo n.º 5
0
        public Model.Korisnici Insert(KorisniciUpsertRequest request)
        {
            var entity = _mapper.Map <Database.Korisnici>(request);

            if (request.Lozinka != request.PotvrdaLozinke)
            {
                throw new Exception("Passwordi se ne slažu");
            }

            entity.LozinkaSalt = GenerateSalt();
            entity.LozinkaHash = GenerateHash(entity.LozinkaSalt, request.Lozinka);

            _context.Korisnici.Add(entity);
            _context.SaveChanges();

            foreach (var uloga in request.Uloge)
            {
                Database.KorisniciUloge korisniciUloge = new Database.KorisniciUloge
                {
                    KorisnikId   = entity.KorisnikId,
                    UlogaId      = uloga,
                    DatumIzmjene = DateTime.Now
                };
                _context.KorisniciUloge.Add(korisniciUloge);
            }
            _context.SaveChanges();

            return(_mapper.Map <Model.Korisnici>(entity));
        }
Exemplo n.º 6
0
        public Model.Korisnici Insert(KorisniciInsertRequest request)
        {
            var entity = _mapper.Map <Database.Korisnici>(request);

            if (request.Password != request.PasswordConfirmation)
            {
                throw new UserException("Passwordi se ne poklapaju!");
            }
            entity.LozinkaSalt = GenerateSalt();
            entity.LozinkaHash = GenerateHash(entity.LozinkaSalt, request.Password);
            _context.Korisnici.Add(entity);
            _context.SaveChanges();
            if (request.Uloge == null)
            {
            }
            foreach (var uloga in request.Uloge)
            {
                Database.KorisniciUloge korisniciUloge = new Database.KorisniciUloge();
                korisniciUloge.KorisnikId   = entity.KorisnikId;
                korisniciUloge.UlogaId      = uloga;
                korisniciUloge.DatumIzmjene = DateTime.Now;
                _context.KorisniciUloge.Add(korisniciUloge);
            }

            _context.SaveChanges();
            return(_mapper.Map <Model.Korisnici>(entity));
        }
Exemplo n.º 7
0
        public Model.Models.Korisnici Insert(KorisniciUpsertRequest request)
        {
            var entity = _mapper.Map <Database.Korisnici>(request);

            if (request.Password != request.PasswordPotvrda)
            {
                throw new Exception("Passwordi se ne slažu");
            }

            entity.LozinkaSalt = GenerateSalt();
            entity.LozinkaHash = GenerateHash(entity.LozinkaSalt, request.Password);

            _context.Korisnici.Add(entity);
            _context.SaveChanges();

            foreach (var uloga in request.Uloge)
            {
                Database.KorisniciUloge korisniciUloge = new Database.KorisniciUloge();
                Database.Uloge          u = _context.Uloge.FirstOrDefault(x => x.UlogaId == uloga);
                korisniciUloge.UlogaId      = u.UlogaId;
                korisniciUloge.KorisnikId   = entity.KorisnikId;
                korisniciUloge.DatumIzmjene = DateTime.Now;
                _context.KorisniciUloge.Add(korisniciUloge);
            }
            _context.SaveChanges();

            return(_mapper.Map <Model.Models.Korisnici>(entity));
        }
Exemplo n.º 8
0
        public Model.Korisnici Insert(KorisniciInsertRequest request)
        {
            var entity = _mapper.Map <Database.Korisnici>(request);

            if (request.Password != request.PasswordPotvrda)
            {
                throw new UserException("Passwordi se ne slažu");
            }

            entity.LozinkaSalt = GenerateSalt();
            entity.LozinkaHash = GenerateHash(entity.LozinkaSalt, request.Password);

            if (request.Pacijent1 != null)
            {
                entity.Pacijent = _mapper.Map <Database.Pacijent>(request.Pacijent1);
            }

            _context.Korisnici.Add(entity);
            _context.SaveChanges();

            foreach (var uloga in request.Uloge)
            {
                Database.KorisniciUloge korisniciUloge = new Database.KorisniciUloge
                {
                    KorisnikId = entity.Id,
                    UlogaId    = uloga
                };
                _context.KorisniciUloge.Add(korisniciUloge);
            }
            _context.SaveChanges();

            return(_mapper.Map <Model.Korisnici>(entity));
        }
Exemplo n.º 9
0
        public Model.Models.Korisnici Update(int Id, KorisniciUpsertRequest request)
        {
            var entity = _context.Korisnici.Include(x => x.KorisniciUloge).FirstOrDefault(x => x.KorisnikId == Id);

            _context.Korisnici.Attach(entity);
            _context.Korisnici.Update(entity);
            request.KorisnikId = entity.KorisnikId;

            if (!string.IsNullOrWhiteSpace(request.Password))
            {
                if (request.Password != request.PasswordPotvrda)
                {
                    throw new Exception("Passwordi se ne slažu");
                }

                entity.LozinkaSalt = GenerateSalt();
                entity.LozinkaHash = GenerateHash(entity.LozinkaSalt, request.Password);
            }


            var trenutneUloge = _context.KorisniciUloge.Where(x => x.KorisnikId == entity.KorisnikId);

            foreach (var uloga in trenutneUloge)
            {
                bool       postoji        = false;
                List <int> sveSelectovane = request.Uloge;
                foreach (var odabrana in sveSelectovane)
                {
                    if (uloga.UlogaId == odabrana)
                    {
                        postoji = true;
                    }
                }
                if (!postoji)
                {
                    _context.KorisniciUloge.Remove(uloga);
                }
            }


            foreach (var uloga in request.Uloge)
            {
                var u = _context.KorisniciUloge.FirstOrDefault(x => x.UlogaId == uloga && x.KorisnikId == entity.KorisnikId);

                if (u == null)
                {
                    Database.KorisniciUloge korisniciUloge = new Database.KorisniciUloge();
                    Database.Uloge          ul             = _context.Uloge.FirstOrDefault(x => x.UlogaId == uloga);
                    korisniciUloge.UlogaId      = ul.UlogaId;
                    korisniciUloge.KorisnikId   = entity.KorisnikId;
                    korisniciUloge.DatumIzmjene = DateTime.Now;
                    _context.KorisniciUloge.Add(korisniciUloge);
                }
            }
            _context.SaveChanges();
            _mapper.Map(request, entity);
            _context.SaveChanges();

            return(_mapper.Map <Model.Models.Korisnici>(entity));
        }
Exemplo n.º 10
0
        public Model.Korisnici Insert(KorisniciUpsertRequest request)
        {
            var korisnik = _mapper.Map <Database.Korisnici>(request);

            if (request.Password != request.PasswordPotvrda)
            {
                throw new Exception("Passwordi se ne slažu");
            }

            korisnik.LozinkaSalt = Util.Helper.GenerateSalt();
            korisnik.LozinkaHash = Util.Helper.GenerateHash(korisnik.LozinkaSalt, request.Password);

            _context.Korisnici.Add(korisnik);
            _context.SaveChanges();

            foreach (var uloga in request.Uloge)
            {
                Database.KorisniciUloge korisniciUloge = new Database.KorisniciUloge();

                korisniciUloge.KorisnikId   = korisnik.KorisnikId;
                korisniciUloge.UlogaId      = uloga;
                korisniciUloge.DatumIzmjene = DateTime.Now;

                _context.KorisniciUloge.Add(korisniciUloge);
            }
            _context.SaveChanges();

            var result = _context.Korisnici.Include(i => i.KorisniciUloge).Where(w => w.KorisnikId == korisnik.KorisnikId).FirstOrDefault();

            return(_mapper.Map <Model.Korisnici>(result));
        }
Exemplo n.º 11
0
        public Model.Korisnici Insert(KorisniciInsertRequest request)
        {
            var entity = _mapper.Map <Database.Korisnici>(request);

            if (request.pass != request.confPass)
            {
                throw new UserException("Pasvordi se ne poklapaju!");
            }


            entity.LozinkaSalt = GenerateSalt();
            entity.LozinkaHash = GenerateHash(entity.LozinkaSalt, request.pass);

            _db.Korisnicis.Add(entity);
            _db.SaveChanges();

            foreach (var item in request.Uloge)
            {
                var korisniciUloga = new Database.KorisniciUloge();
                korisniciUloga.DatumIzmjene = DateTime.Now;
                korisniciUloga.KorisnikId   = entity.KorisnikId;
                korisniciUloga.UlogaId      = item;
                _db.KorisniciUloges.Add(korisniciUloga);
            }

            _db.SaveChanges();

            return(_mapper.Map <Model.Korisnici>(entity));
        }
Exemplo n.º 12
0
        public KorisnickiNalog Update(int id, KorisnickiNalogUpsertRequest request)
        {
            var entity = _context.KorisnickiNalog.Find(id);

            _context.KorisnickiNalog.Attach(entity);
            _context.KorisnickiNalog.Update(entity);

            foreach (var nalog in _context.KorisnickiNalog.ToList())
            {
                if (nalog.Username == request.Username && nalog.Username != nalog.Username)
                {
                    throw new UserException("Username je zauzet!");
                }
            }

            if (request.Password != null)
            {
                if (request.Password != request.PasswordConfirmation)
                {
                    throw new UserException("Passwords are not matching");
                }
                entity.PasswordSalt = GenerateSalt();
                entity.PasswordHash = GenerateHash(entity.PasswordSalt, request.Password);
            }

            entity.Username = request.Username;
            var entityUloge = _context.KorisniciUloge.Where(c => c.KorisnickiNalogId == entity.KorisnickiNalogId).ToList();

            foreach (var postojuceUloge in entityUloge)
            {
                _context.KorisniciUloge.Remove(postojuceUloge);
            }

            _context.SaveChanges();

            foreach (var novaUloga in request.Uloge)
            {
                Database.KorisniciUloge korisniciUloge = new Database.KorisniciUloge();

                korisniciUloge.KorisnickiNalogId = entity.KorisnickiNalogId;
                korisniciUloge.UlogaId           = novaUloga;
                korisniciUloge.DatumIzmjene      = DateTime.Now;

                _context.KorisniciUloge.Add(korisniciUloge);
            }

            _context.SaveChanges();
            return(_mapper.Map <Model.KorisnickiNalog>(entity));
        }
Exemplo n.º 13
0
        public Model.Korisnici Update(int id, KorisniciUpsertRequest request)
        {
            var entity = _context.Korisnici.Find(id);

            _context.Korisnici.Attach(entity);
            _context.Korisnici.Update(entity);

            if (!string.IsNullOrWhiteSpace(request.Lozinka))
            {
                if (request.Lozinka != request.PotvrdaLozinke)
                {
                    throw new Exception("Passwordi se ne slažu");
                }

                entity.LozinkaSalt = GenerateSalt();
                entity.LozinkaHash = GenerateHash(entity.LozinkaSalt, request.Lozinka);
            }


            var korisnickeUloge = _context.KorisniciUloge.Where(x => x.KorisnikId == id).ToList();

            foreach (var item in korisnickeUloge)
            {
                _context.KorisniciUloge.Remove(item);
            }
            _context.SaveChanges();

            foreach (var uloga in request.Uloge)
            {
                Database.KorisniciUloge korisniciUloge = new Database.KorisniciUloge
                {
                    KorisnikId   = entity.KorisnikId,
                    UlogaId      = uloga,
                    DatumIzmjene = DateTime.Now
                };
                _context.KorisniciUloge.Add(korisniciUloge);
            }
            _context.SaveChanges();


            _mapper.Map(request, entity);

            _context.SaveChanges();

            return(_mapper.Map <Model.Korisnici>(entity));
        }
Exemplo n.º 14
0
        public Model.Korisnici Insert(KorisniciInsertRequest request)
        {
            var entity = _mapper.Map <Database.Korisnik>(request);

            if (request.Password != request.PasswordPotvrda)
            {
                throw new Exception("Passwordi se ne slažu");
            }

            entity.LozinkaSalt = GenerateSalt();
            entity.LozinkaHash = GenerateHash(entity.LozinkaSalt, request.Password);

            _context.Korisnik.Add(entity);
            _context.SaveChanges();

            foreach (var uloga in request.Uloge)
            {
                Database.KorisniciUloge korisniciUloge = new Database.KorisniciUloge();
                korisniciUloge.KorisnikId   = entity.KorisnikId;
                korisniciUloge.UlogaId      = uloga;
                korisniciUloge.DatumIzmjene = DateTime.Now;
                _context.KorisniciUloge.Add(korisniciUloge);
            }
            _context.SaveChanges();
            var rezultat = new Model.Korisnici();

            rezultat.Ime           = entity.Ime;
            rezultat.Prezime       = entity.Prezime;
            rezultat.KorisnikId    = entity.KorisnikId;
            rezultat.Email         = entity.Email;
            rezultat.Telefon       = entity.Telefon;
            rezultat.Status        = entity.Status;
            rezultat.KorisnickoIme = entity.KorisnickoIme;
            rezultat.Slika         = entity.Slika;

            return(rezultat);
        }
Exemplo n.º 15
0
        public OnlineGym_Model.Korisnik Update(int id, KorisniciInsertRequest request)
        {
            var query         = _context.Set <Database.Korisnik>().Include(c => c.KorisniciUloge).AsQueryable();
            var entity        = query.Where(x => x.KorisnikId == id).FirstOrDefault();
            var entityRequest = _mapper.Map <Database.Korisnik>(request);


            if (entity != null)
            {
                if (request.StariPassword != null)
                {
                    var newHash = GenerateHash(entity.LozinkaSalt, request.StariPassword);

                    if (newHash != entity.LozinkaHash)
                    {
                        return(null);
                        //throw new Exception("Unesite ispravnu staru lozinku!");
                    }
                    else
                    {
                        _context.Korisnik.Attach(entity);
                        _context.Korisnik.Update(entity);

                        _context.SaveChanges();


                        if (!string.IsNullOrWhiteSpace(request.Password))
                        {
                            if (request.Password != request.PasswordPotvrda)
                            {
                                throw new Exception("Passwordi se ne slažu");
                            }

                            entity.LozinkaSalt = GenerateSalt();
                            entity.LozinkaHash = GenerateHash(entity.LozinkaSalt, request.Password);
                        }

                        _mapper.Map(request, entity);

                        _context.SaveChanges();

                        return(_mapper.Map <OnlineGym_Model.Korisnik>(entity));
                    }
                }
                else
                {
                    _context.Korisnik.Attach(entity);
                    _context.Korisnik.Update(entity);

                    foreach (var postojecaUloga in entity.KorisniciUloge)
                    {
                        _context.KorisniciUloge.Remove(postojecaUloga);
                    }
                    _context.SaveChanges();


                    foreach (var novaUloga in request.Uloge)
                    {
                        Database.KorisniciUloge korisniciUloge = new Database.KorisniciUloge();
                        korisniciUloge.KorisnikId = entity.KorisnikId;
                        korisniciUloge.UlogaId    = novaUloga;
                        _context.KorisniciUloge.Add(korisniciUloge);
                    }
                    _context.SaveChanges();
                    _context.SaveChanges();

                    if (!string.IsNullOrWhiteSpace(request.Password))
                    {
                        if (request.Password != request.PasswordPotvrda)
                        {
                            throw new Exception("Passwordi se ne slažu");
                        }

                        entity.LozinkaSalt = GenerateSalt();
                        entity.LozinkaHash = GenerateHash(entity.LozinkaSalt, request.Password);
                    }

                    _mapper.Map(request, entity);

                    _context.SaveChanges();

                    return(_mapper.Map <OnlineGym_Model.Korisnik>(entity));
                }
            }
            return(null);
        }