Exemplo n.º 1
0
        public async Task <ApplicationUserModel> Register(ApplicationUserInsertModel model)
        {
            try
            {
                if (!(IsUsernameUnique(model.Username)))
                {
                    throw new Exception("Username je već zauzet.");
                }
                var user = new ApplicationUser
                {
                    UserName = model.Username,
                    Active   = true
                };
                await _userManager.CreateAsync(user, model.Password);

                await _userManager.AddToRoleAsync(user, model.RoleNaziv);

                var created = new ApplicationUserModel
                {
                    Username = user.UserName
                };
                return(created);
            }
            catch
            {
                throw;
            }
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Register([FromBody] ApplicationUserInsertModel model)
        {
            try
            {
                if (model.Username.Contains(":"))
                {
                    ModelState.AddModelError("Username", "Ne smije sadržavati ':'");
                }
                if (model.Password.Contains(":"))
                {
                    ModelState.AddModelError("Password", "Ne smije sadržavati ':'");
                }
                if (!ModelState.IsValid)
                {
                    return(BadRequest(new Exception("Neispravan unos podataka.")));
                }
                var returnModel = await _userService.AddKlijent(model);

                return(Ok(returnModel));
            }
            catch (Exception ex)
            {
                return(BadRequest(new ApiException(ex.Message, HttpStatusCode.BadRequest)));
            }
        }
 public RegisterViewModel()
 {
     RegisterCommand = new Command(async() => {
         try
         {
             if (FormNotValid())
             {
                 return;
             }
             var noviKorisnik = new ApplicationUserInsertModel
             {
                 DatumRodjenja   = DatumRodjenja,
                 Email           = Email,
                 Ime             = Ime,
                 JMBG            = JMBG,
                 OpcinaId        = Opcina.Id,
                 Password        = Password,
                 PasswordConfirm = PasswordConfirm,
                 Prezime         = Prezime,
                 Spol            = Spol,
                 Username        = Username
             };
             var korisnik = await _userService.Insert <KlijentModel>(noviKorisnik);
             if (korisnik != null)
             {
                 await Application.Current.MainPage.DisplayAlert("", "Uspješno ste izvršili registraciju, sada se možete logirati.", "OK");
                 //Application.Current.MainPage = new LoginPage();
                 await Navigation.PopModalAsync();
             }
         }
         catch {}
     });
     GetOpcineDataCommand = new Command(async() =>
     {
         await GetOpcine();
     });
     GoBackCommand = new Command(async() =>
     {
         await Navigation.PopModalAsync();
     });
     GetOpcineDataCommand.Execute(null);
 }
        public async Task <KlijentModel> AddKlijent(ApplicationUserInsertModel model)
        {
            try
            {
                if (model.Password != model.PasswordConfirm)
                {
                    throw new Exception("Lozinke nisu iste.");
                }
                if (!(IsUsernameUnique(model.Username)))
                {
                    throw new Exception("Username je već zauzet.");
                }
                var user = new ApplicationUser
                {
                    Email             = model.Email,
                    PasswordSalt      = UserAuthHelpers.GenerateSalt(),
                    Username          = model.Username,
                    DatumRodjenja     = model.DatumRodjenja,
                    Ime               = model.Ime,
                    JMBG              = model.JMBG,
                    OpcinaId          = model.OpcinaId,
                    Prezime           = model.Prezime,
                    Spol              = model.Spol,
                    DatumRegistracije = DateTime.Now,
                    Active            = true
                };
                if (model.Slika != null && model.Slika.Length > 0)
                {
                    user.Slika = model.Slika;
                }
                user.PasswordHash = UserAuthHelpers.GenerateHash(user.PasswordSalt, model.Password);
                _context.ApplicationUser.Add(user);

                var roleKlijent = _context.Role.Where(r => r.Naziv == "Klijent").First();
                _context.ApplicationUserRole.Add(new ApplicationUserRole
                {
                    ApplicationUser = user,
                    Role            = roleKlijent
                });
                var klijent = new Klijent {
                    ApplicationUser = user
                };
                _context.Klijent.Add(klijent);
                await _context.SaveChangesAsync();

                var created = new KlijentModel
                {
                    DatumRodjenja = user.DatumRodjenja,
                    Email         = user.Email,
                    Id            = user.Id,
                    Ime           = user.Ime,
                    JMBG          = user.JMBG,
                    OpcinaId      = user.OpcinaId,
                    Prezime       = user.Prezime,
                    Spol          = user.Spol,
                    Slika         = user.Slika,
                    Username      = user.Username,
                    Role          = new List <RoleModel> {
                        new RoleModel {
                            Id = roleKlijent.Id, Naziv = roleKlijent.Naziv
                        }
                    },
                    KlijentId = klijent.Id
                };
                created.OpcinaNaziv = _context.Opcina
                                      .Find(created.OpcinaId)
                                      .Naziv;
                return(created);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }