Exemplo n.º 1
0
        public int CreateOrUpdateUser([FromUri]User user)
        {
            using (var context = new TravelPlannerEntities())
            {
                var loggedUser = UserHelper.GetLoggedUser(Request);

                var loggedRole = context.Role.First(x => x.Id == loggedUser.RoleId);

                if (loggedRole.Name == RolesEnum.User.ToString())
                {
                    throw new WebException(Messages.Unauthorized);
                }

                var validationMessage = ValidateUserData(user);
                if (!string.IsNullOrEmpty(validationMessage))
                {
                    throw new WebException(validationMessage);
                }

                if (user.Id == 0)
                {
                    context.User.Add(user);
                }
                else
                {
                    context.Entry(user).State = EntityState.Modified;
                }
                context.SaveChanges();
                return user.Id;
            }
        }
Exemplo n.º 2
0
        private TokenResponse CreateToken(User user, TravelPlannerEntities context)
        {
            var dateTime = DateTime.UtcNow;
            var time = BitConverter.GetBytes(dateTime.ToBinary());
            var key = Guid.NewGuid().ToByteArray();
            var token = Convert.ToBase64String(time.Concat(key).ToArray());

            user.Token = token;
            context.Entry(user).State = EntityState.Modified;
            context.SaveChanges();
            return new TokenResponse
            {
                Token = token,
                ExpirationDate = dateTime.AddHours(24).ToString("MM/dd/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture),
                Role = user.Role.Name
            };
        }