コード例 #1
0
        public UserRoleGetModel Create(UserRolePostModel userRolePostModel)
        {
            UserRole toAdd = UserRolePostModel.ToUserRole(userRolePostModel);

            context.UserRoles.Add(toAdd);
            context.SaveChanges();
            return(UserRoleGetModel.FromUserRole(toAdd));
        }
コード例 #2
0
        public PachetGetModel Create(PachetPostModel pachetModel)
        {
            Pachet toAdd = PachetPostModel.ToPachet(pachetModel);

            context.Pachete.Add(toAdd);
            context.SaveChanges();
            return(PachetGetModel.FromPachet(toAdd));
        }
コード例 #3
0
        public Film Create(FilmPostModel film, User addedBy)
        {
            Film toAdd = FilmPostModel.ToFilm(film);

            toAdd.Owner = addedBy;      //adaugam persoana care a adaugat acest Film Film
            context.Filme.Add(toAdd);
            context.SaveChanges();
            return(toAdd);
        }
コード例 #4
0
        public ActionResult Create([Bind(Include = "Id,Nome,Ano,Descricao,Autor")] AlbumViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                Album album = Mapper.Map <AlbumViewModel, Album>(viewModel);
                db.Albuns.Add(album);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(viewModel));
        }
コード例 #5
0
        public void CreateShouldReturnNullIfValidUserGetModel()
        {
            var options = new DbContextOptionsBuilder <FilmeDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(CreateShouldReturnNullIfValidUserGetModel))
                          .Options;

            using (var context = new FilmeDbContext(options))
            {
                var validator    = new RegisterValidator();
                var usersService = new UsersService(context, validator, null, config);

                UserRole addUserRoleRegular = new UserRole
                {
                    Name        = "Regular",
                    Description = "Creat pentru testare"
                };
                context.UserRoles.Add(addUserRoleRegular);
                context.SaveChanges();

                var added1 = new Lab3.ViewModels.RegisterPostModel
                {
                    FirstName = "firstName1",
                    LastName  = "firstName1",
                    UserName  = "******",
                    Email     = "*****@*****.**",
                    Password  = "******"
                };

                var userCreated = usersService.Create(added1);

                Assert.IsNull(userCreated);
            }
        }
コード例 #6
0
        public void GetByIdShouldReturnUserRole()
        {
            var options = new DbContextOptionsBuilder <FilmeDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(GetByIdShouldReturnUserRole))
                          .Options;

            using (var context = new FilmeDbContext(options))
            {
                var userUserRolesService = new UserUserRolesService(null, context);

                User userToAdd = new User
                {
                    Email          = "*****@*****.**",
                    LastName       = "Ion",
                    FirstName      = "POpescu",
                    Password       = "******",
                    DataRegistered = DateTime.Now,
                    UserUserRoles  = new List <UserUserRole>()
                };
                context.Users.Add(userToAdd);

                UserRole addUserRole = new UserRole
                {
                    Name        = "Rol testare",
                    Description = "Creat pentru testare"
                };
                context.UserRoles.Add(addUserRole);
                context.SaveChanges();

                context.UserUserRoles.Add(new UserUserRole
                {
                    User      = userToAdd,
                    UserRole  = addUserRole,
                    StartTime = DateTime.Now,
                    EndTime   = null
                });
                context.SaveChanges();

                var userUserRoleGetModels = userUserRolesService.GetHistoryRoleById(1);
                Assert.IsNotNull(userUserRoleGetModels.FirstOrDefaultAsync(uur => uur.EndTime == null));
            }
        }
コード例 #7
0
        public void GetUserRoleNameByIdShouldReturnUserRoleName()
        {
            var options = new DbContextOptionsBuilder <FilmeDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(GetUserRoleNameByIdShouldReturnUserRoleName))
                          .Options;

            using (var context = new FilmeDbContext(options))
            {
                var userUserRolesService = new UserUserRolesService(null, context);

                User userToAdd = new User
                {
                    Email          = "*****@*****.**",
                    LastName       = "Ion",
                    FirstName      = "POpescu",
                    Password       = "******",
                    DataRegistered = DateTime.Now,
                    UserUserRoles  = new List <UserUserRole>()
                };
                context.Users.Add(userToAdd);

                UserRole addUserRole = new UserRole
                {
                    Name        = "Regular",
                    Description = "Creat pentru testare"
                };
                context.UserRoles.Add(addUserRole);
                context.SaveChanges();

                context.UserUserRoles.Add(new UserUserRole
                {
                    User      = userToAdd,
                    UserRole  = addUserRole,
                    StartTime = DateTime.Parse("2019-06-13T00:00:00"),
                    EndTime   = null
                });
                context.SaveChanges();

                string userRoleName = userUserRolesService.GetUserRoleNameById(1);
                Assert.AreEqual("Regular", userRoleName);
            }
        }
コード例 #8
0
        public LoginGetModel Register(RegisterPostModel registerinfo)
        {
            User existing = context.Users.FirstOrDefault(u => u.Username == registerinfo.UserName);

            if (existing != null)
            {
                return(null);
            }
            context.Users.Add(new User
            {
                Email     = registerinfo.Email,
                LastName  = registerinfo.LastName,
                FirstName = registerinfo.FirstName,
                Password  = ComputeSha256Hash(registerinfo.Password),
                Username  = registerinfo.UserName,
                UserRole  = UserRole.Regular
            });
            context.SaveChanges();
            return(Authenticate(registerinfo.UserName, registerinfo.Password));
        }
コード例 #9
0
        public ErrorsCollection Register(RegisterPostModel registerinfo)
        {
            var errors = registerValidator.Validate(registerinfo, context);

            if (errors != null)
            {
                return(errors);
            }

            User toAdd = new User
            {
                Email          = registerinfo.Email,
                LastName       = registerinfo.LastName,
                FirstName      = registerinfo.FirstName,
                Password       = ComputeSha256Hash(registerinfo.Password),
                Username       = registerinfo.UserName,
                DataRegistered = DateTime.Now,
                UserUserRoles  = new List <UserUserRole>()
            };

            //se atribuie rolul de Regular ca default
            var regularRole = context
                              .UserRoles
                              .FirstOrDefault(ur => ur.Name == UserRoles.Regular);

            context.Users.Add(toAdd);
            context.UserUserRoles.Add(new UserUserRole
            {
                User      = toAdd,
                UserRole  = regularRole,
                StartTime = DateTime.Now,
                EndTime   = null
            });

            context.SaveChanges();
            return(null);
        }
コード例 #10
0
        public void AuthenticateShouldLogTheRegisteredUser()
        {
            var options = new DbContextOptionsBuilder <FilmeDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(AuthenticateShouldLogTheRegisteredUser))
                          .Options;

            using (var context = new FilmeDbContext(options))
            {
                var validator           = new RegisterValidator();
                var validatorUser       = new UserRoleValidator();
                var userUserRoleService = new UserUserRolesService(validatorUser, context);
                var usersService        = new UsersService(context, validator, userUserRoleService, config);

                UserRole addUserRoleRegular = new UserRole
                {
                    Name        = "Regular",
                    Description = "Creat pentru testare"
                };
                context.UserRoles.Add(addUserRoleRegular);
                context.SaveChanges();

                var added = new Lab3.ViewModels.RegisterPostModel
                {
                    FirstName = "firstName1",
                    LastName  = "lastName1",
                    UserName  = "******",
                    Email     = "*****@*****.**",
                    Password  = "******"
                };
                var result = usersService.Register(added);

                var authenticated = new Lab3.ViewModels.LoginPostModel
                {
                    Username = "******",
                    Password = "******"
                };
                //valid authentification
                var authresult = usersService.Authenticate(added.UserName, added.Password);

                Assert.IsNotNull(authresult);
                Assert.AreEqual(1, authresult.Id);
                Assert.AreEqual(authenticated.Username, authresult.UserName);

                //invalid user authentification
                var authresult1 = usersService.Authenticate("unknown", "abcdefg");
                Assert.IsNull(authresult1);
            }
        }
コード例 #11
0
        public async Task <IActionResult> Create(Filme filmes, List <IFormFile> Imagem)
        {
            foreach (var item in Imagem)
            {
                if (item.Length > 0)
                {
                    using (var stream = new MemoryStream())
                    {
                        await item.CopyToAsync(stream);

                        filmes.Imagem = stream.ToArray();
                    }
                }
            }
            _context.Filmes.Add(filmes);
            _context.SaveChanges();
            return(RedirectToAction("Index"));
        }
コード例 #12
0
        public void GetCurentUserShouldReturnAccesToKlaims()
        {
            var options = new DbContextOptionsBuilder <FilmeDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(GetCurentUserShouldReturnAccesToKlaims))
                          .Options;

            using (var context = new FilmeDbContext(options))
            {
                var validator           = new RegisterValidator();
                var validatorUser       = new UserRoleValidator();
                var userUserRoleService = new UserUserRolesService(validatorUser, context);
                var usersService        = new UsersService(context, validator, userUserRoleService, config);

                UserRole addUserRoleRegular = new UserRole
                {
                    Name        = "Regular",
                    Description = "Creat pentru testare"
                };
                context.UserRoles.Add(addUserRoleRegular);
                context.SaveChanges();

                var added = new Lab3.ViewModels.RegisterPostModel
                {
                    FirstName = "firstName1",
                    LastName  = "lastName1",
                    UserName  = "******",
                    Email     = "*****@*****.**",
                    Password  = "******"
                };
                var result = usersService.Register(added);

                var authenticated = new Lab3.ViewModels.LoginPostModel
                {
                    Username = "******",
                    Password = "******"
                };
                var authresult = usersService.Authenticate(added.UserName, added.Password);

                //nu stiu sa instantiez un HttpContext
                //usersService.GetCurentUser(httpContext);

                Assert.IsNotNull(authresult);
            }
        }
コード例 #13
0
        public void CreateShouldAddTheUserUserRole()
        {
            var options = new DbContextOptionsBuilder <FilmeDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(CreateShouldAddTheUserUserRole))
                          .Options;

            using (var context = new FilmeDbContext(options))
            {
                var validator            = new UserRoleValidator();
                var userUserRolesService = new UserUserRolesService(validator, context);

                User userToAdd = new User
                {
                    Email          = "*****@*****.**",
                    LastName       = "Ion",
                    FirstName      = "POpescu",
                    Password       = "******",
                    DataRegistered = DateTime.Now,
                    UserUserRoles  = new List <UserUserRole>()
                };
                context.Users.Add(userToAdd);

                UserRole addUserRoleRegular = new UserRole
                {
                    Name        = "Regular",
                    Description = "Creat pentru testare"
                };
                UserRole addUserRoleAdmin = new UserRole
                {
                    Name        = "AdminDeTest",
                    Description = "Creat pentru testare"
                };
                context.UserRoles.Add(addUserRoleRegular);
                context.UserRoles.Add(addUserRoleAdmin);
                context.SaveChanges();

                context.UserUserRoles.Add(new UserUserRole
                {
                    User      = userToAdd,
                    UserRole  = addUserRoleRegular,
                    StartTime = DateTime.Parse("2019-06-13T00:00:00"),
                    EndTime   = null
                });
                context.SaveChanges();

                //sectiunea de schimbare valori invalidata de catre UserRoleValidator
                var uurpm = new UserUserRolePostModel
                {
                    UserId       = userToAdd.Id,
                    UserRoleName = "Admin"
                };
                var result1 = userUserRolesService.Create(uurpm);
                Assert.IsNotNull(result1);   //User role nu exista in baza de date dupa validare, ==> exista erori la validare

                //sectiunea de schimbare valori validata de catre UserRoleValidator
                var uurpm1 = new UserUserRolePostModel
                {
                    UserId       = userToAdd.Id,
                    UserRoleName = "AdminDeTest"
                };
                var result2 = userUserRolesService.Create(uurpm1);
                Assert.IsNull(result2);   //User role exista si se face upsert
            }
        }
コード例 #14
0
        public ErrorsCollection Create(UserUserRolePostModel userUserRolePostModel)
        {
            var errors = userRoleValidator.Validate(userUserRolePostModel, context);

            if (errors != null)
            {
                return(errors);
            }

            User user = context.Users
                        .FirstOrDefault(u => u.Id == userUserRolePostModel.UserId);

            if (user != null)
            {
                UserRole userRole = context
                                    .UserRoles
                                    .Include(ur => ur.UserUserRoles)
                                    .FirstOrDefault(ur => ur.Name == userUserRolePostModel.UserRoleName);

                UserUserRole curentUserUserRole = context.UserUserRoles
                                                  .Include(uur => uur.UserRole)
                                                  .FirstOrDefault(uur => uur.UserId == user.Id && uur.EndTime == null);

                //discutabil, nu ar trebui sa fie null niciodata, adica la Register nu a fost creat bine un defalut Regular role pentru userul respectiv
                if (curentUserUserRole == null)
                {
                    context.UserUserRoles.Add(new UserUserRole
                    {
                        User      = user,
                        UserRole  = userRole,
                        StartTime = DateTime.Now,
                        EndTime   = null
                    });

                    context.SaveChanges();
                    return(null);
                }

                //inchiderea perioadel de activare pentru un anumit rol
                if (!curentUserUserRole.UserRole.Name.Contains(userUserRolePostModel.UserRoleName))
                {
                    curentUserUserRole.EndTime = DateTime.Now;

                    context.UserUserRoles.Add(new UserUserRole
                    {
                        User      = user,
                        UserRole  = userRole,
                        StartTime = DateTime.Now,
                        EndTime   = null
                    });

                    context.SaveChanges();
                    return(null);
                }
                else
                {
                    return(null);    //trebuie sa trimit eroare ca modificarea nu poate avea loc, rol nou = rol vechi
                }
            }
            return(null);    //eroare Nu exista User cu Id-ul specificat
        }
コード例 #15
0
 public void Add(Filme filme)
 {
     _contexto.Filmes.Add(filme);
     _contexto.SaveChanges();
 }