Esempio n. 1
0
        public string Handle(LoginQuery query, IWebHydraContext context)
        {
            Console.WriteLine($"Loging with '{query.Email}' and '{query.Password}'...");

            // TODO validation

            UserEntity userEntity = _users.GetByEmail(query.Email);

            if (userEntity == null)
            {
                throw new UserNotFoundException();
            }

            if (userEntity.Password != query.Password)
            {
                throw new WrongPasswordException();
            }

            User user = new User();

            user.Id     = userEntity.Id;
            user.Claims = JsonConvert.DeserializeObject <Claims>(userEntity.Claims);

            return(_auth.GenerateTokenForUser(user));
        }
Esempio n. 2
0
        public string Handle(RegisterQuery query, IWebHydraContext context)
        {
            Console.WriteLine($"Registering with '{query.Email}' and '{query.Password}'...");

            // TODO validation

            UserEntity user = _users.GetByEmail(query.Email);

            if (user != null)
            {
                throw new UserAlreadyExistsException();
            }

            UserEntity userEntity = new UserEntity()
            {
                Claims   = JsonConvert.SerializeObject(new Claims()),
                Id       = new Guid(),
                Email    = query.Email,
                Password = query.Password
            };

            _users.AddUser(userEntity);

            User user2 = new User();

            user2.Id     = userEntity.Id;
            user2.Claims = JsonConvert.DeserializeObject <Claims>(userEntity.Claims);

            return(_auth.GenerateTokenForUser(user2));
        }
Esempio n. 3
0
        public void Handle(DeleteNotesCommand command, IWebHydraContext context)
        {
            if (!context.User.HasClaim(c => c.CanDeleteNote))
            {
                throw new AuthorizationException();
            }

            _notes.DeleteNote(command.Id);
        }
        public void Handle(AddNoteCommand command, IWebHydraContext context)
        {
            if (!context.User.HasClaim(c => c.CanAddNote))
            {
                throw new AuthorizationException();
            }

            Note note = Mapper.Map <Note>(command);

            note.UserId = context.User.Id;

            _notes.AddNote(note);
        }
Esempio n. 5
0
        public object Execute(IMessage message, IWebHydraContext context)
        {
            dynamic handler = GetHandler(message);

            if (message is IQueryBase)
            {
                return(handler.Handle((dynamic)message, context));
            }

            handler.Handle((dynamic)message, context);

            return(null);
        }
        public NoteDto[] Handle(GetNotesQuery query, IWebHydraContext context)
        {
            Console.WriteLine($"Fetching tab '{query.Id}'...");

            if (!context.User.HasClaim(c => c.CanReadNote))
            {
                throw new AuthorizationException();
            }

            IEnumerable <Note> notes = _notes.GetNotes(query.Id, context.User.Id);

            IEnumerable <NoteDto> notesDtos = Mapper.Map <IEnumerable <Note>, IEnumerable <NoteDto> >(notes);

            return(notesDtos.ToArray());
        }
        public void Handle(UpdateNoteCommand command, IWebHydraContext context)
        {
            if (!context.User.HasClaim(c => c.CanChangeNote))
            {
                throw new AuthorizationException();
            }

            if (command.tab == null)
            {
                throw new Exception("empty tab");
            }

            Note note = Mapper.Map <NoteDto, Note>(command.tab);

            if (note == null)
            {
                throw new Exception("mapping problem");
            }

            _notes.UpdateNote(note);
        }