//private DbItem GetUserItem(TcpConnectedHost host)
        //{
        //    return _authenticationProviders.Where(p => p.IsAuthenticated(host)).Select(p => p.GetUserItem(host)).FirstOrDefault();
        //}

        public void RegisterTo(TcpServer server)
        {
            _dataService.ItemChanged += delegate(int id)
            {
                var message = new ObjectTcpMessage <ItemChangeEvent>(TcpRequestType.List);
                message.Add(new ItemChangeEvent {
                    Id = id
                });
                server.Enqueue(message);
            };

            _dataService.RelationAdded += delegate(DbRelation relation)
            {
                var message = new ObjectTcpMessage <DbRelation>(TcpRequestType.InsertRelation);
                message.Add(relation);
                server.Enqueue(message);
            };

            _dataService.RelationRemoved += delegate(DbRelation relation)
            {
                var message = new ObjectTcpMessage <DbRelation>(TcpRequestType.DeleteRelation);
                message.Add(relation);
                server.Enqueue(message);
            };

            server.Register(HandleMultiRelationQuery);
            server.Register(HandleUniqueRelationQuery);
            server.Register(HandleInsertRelation);
            server.Register(HandleDeleteRelation);
            server.Register(HandleUniqueItemQuery);
            server.Register(HandleInsertItem);
            server.Register(HandleUpdateItem);
            server.Register(HandleDeleteItem);
        }
        private TcpMessage HandleMultiRelationQuery(TcpConnectedHost host, TcpMessage message)
        {
            if (!message.HasType(TcpRequestType.MultiRelationQuery))
            {
                return(null);
            }

            if (!IsAuthenticated(host))
            {
                throw AuthException.NotLoggedIn();
            }

            var request = new ParamTcpMessage(message);

            var response = new ObjectTcpMessage <DbRelation>(TcpRequestType.List);

            if (request.HasParam("from_id"))
            {
                var fromId = request.GetParamAsNullableInt("from_id");
                var items  = _dataService.GetRelations(fromId);
                response.Add(items);
                return(response);
            }

            return(TcpMessage.Error("Missing or badly formatted query parameters"));
        }
        private TcpMessage HandleInsertItem(TcpConnectedHost host, TcpMessage message)
        {
            if (!message.HasType(TcpRequestType.InsertItem))
            {
                return(null);
            }

            if (!IsAuthenticated(host))
            {
                throw AuthException.NotLoggedIn();
            }

            var request = new ObjectTcpMessage <DbItem>(message);

            var response = new ObjectTcpMessage <DbItem>(TcpRequestType.List);
            var item     = request.Item;

            if (item != null)
            {
                _dataService.Insert(item);
                response.Add(item); // Send the item back to report assigned Id
            }

            return(response);
        }
Exemple #4
0
        public override void Delete(DbRelation relation)
        {
            var request = new ObjectTcpMessage <DbRelation>(TcpRequestType.DeleteRelation);

            request.Add(relation);
            Client.SendAndReceive(request);
        }
        private TcpMessage HandleUniqueItemQuery(TcpConnectedHost host, TcpMessage message)
        {
            if (!message.HasType(TcpRequestType.UniqueItemQuery))
            {
                return(null);
            }

            if (!IsAuthenticated(host))
            {
                throw AuthException.NotLoggedIn();
            }

            var request = new ParamTcpMessage(message);

            var response = new ObjectTcpMessage <DbItem>(TcpRequestType.List);

            if (request.HasParam("id"))
            {
                var id = request.GetParamAsNullableInt("id");
                if (id != null)
                {
                    var item = _dataService.GetItem(id.Value);
                    response.Add(item);
                    return(response);
                }
            }

            return(TcpMessage.Error("Missing or badly formatted query parameters"));
        }
Exemple #6
0
        public void Login(string username, string password)
        {
            var request = new ParamTcpMessage("loginbasic");

            request.SetParam("username", username);
            request.SetParam("password", password);
            var response = _client.SendAndReceive(request);

            if (response.HasType(TcpRequestType.Ok))
            {
                var objResponse = new ObjectTcpMessage <DbItem>(response);
                if (objResponse.Item != null)
                {
                    IsAuthenticated = true;

                    UserItemId = objResponse.Item.Id;

                    UserWorkspaceContainerId = _dataService.GetOrCreateItem(UserItemId, "workspace").Id;
                }
                else
                {
                    throw new AuthException("Login failed. No user-item returned.");
                }
            }
            else
            {
                throw new AuthException("Login failed. Response: " + response.RequestType + " - " + response.Content);
            }
        }
Exemple #7
0
        public override void Delete(DbItem item)
        {
            var request = new ObjectTcpMessage <DbItem>(TcpRequestType.DeleteItem);

            request.Add(item);
            Client.SendAndReceive(request);
        }
        private TcpMessage HandleLoginRequest(TcpConnectedHost host, TcpMessage message)
        {
            if (!message.HasType("loginbasic"))
            {
                return(null);
            }

            var request = new ParamTcpMessage(message);

            var username = request.GetParam("username");
            var password = request.GetParam("password");

            if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
            {
                var userItem = _dataService.GetItemByRelation(UsersParentId, username);

                if (userItem == null)
                {
                    if (!AutoRegisterUsers)
                    {
                        return(TcpMessage.Error("User not found. Automatic registering of users is currently disabled."));
                    }

                    // Register
                    userItem = new DbItem(username);
                    _dataService.Insert(userItem);
                    _dataService.Insert(new DbRelation(UsersParentId, username, userItem.Id));

                    var saltItem = new DbItem(HashHelper.CreateSaltString(10));
                    _dataService.Insert(saltItem);
                    _dataService.Insert(new DbRelation(userItem.Id, "salt", saltItem.Id));

                    var passwordItem = new DbItem(HashHelper.GenerateSaltedHash(password, saltItem.Value));
                    _dataService.Insert(passwordItem);
                    _dataService.Insert(new DbRelation(userItem.Id, "password", passwordItem.Id));
                }
                else
                {
                    // Login
                    var saltItem     = _dataService.GetItemByRelation(userItem.Id, "salt");
                    var passwordItem = _dataService.GetItemByRelation(userItem.Id, "password");

                    if (saltItem == null || saltItem.Value == null || passwordItem == null || passwordItem.Value == null || !HashHelper.ConfirmPassword(passwordItem.Value, password, saltItem.Value))
                    {
                        return(TcpMessage.Error("Login failed. Wrong password."));
                    }
                }

                _hostUsers[host] = userItem;

                var response = new ObjectTcpMessage <DbItem>(TcpRequestType.Ok);
                response.Add(userItem);
                return(response);
            }

            return(TcpMessage.Error("Missing or badly formatted login parameters"));
        }
Exemple #9
0
        public override void Insert(DbRelation relation)
        {
            var request = new ObjectTcpMessage <DbRelation>(TcpRequestType.InsertRelation);

            request.Add(relation);
            var response         = Client.SendAndReceive <DbRelation>(request);
            var responseRelation = response.Item;

            if (responseRelation != null)
            {
                relation.Id = responseRelation.Id;
            }
        }
Exemple #10
0
        public override void Insert(DbItem item)
        {
            var request = new ObjectTcpMessage <DbItem>(TcpRequestType.InsertItem);

            request.Add(item);
            var response     = Client.SendAndReceive <DbItem>(request);
            var responseItem = response.Item;

            if (responseItem != null)
            {
                item.Id = responseItem.Id;
            }
        }
Exemple #11
0
        private bool HandleRelationRemoved(TcpMessage request)
        {
            if (!request.HasType(TcpRequestType.DeleteRelation))
            {
                return(false);
            }

            var relation = new ObjectTcpMessage <DbRelation>(request).Item;

            if (relation != null)
            {
                OnRelationRemoved(relation);
            }

            return(true);
        }
Exemple #12
0
        private bool HandleItemChanged(TcpMessage request)
        {
            if (!request.HasType(TcpRequestType.List))
            {
                return(false);
            }

            var items = new ObjectTcpMessage <ItemChangeEvent>(request).Items;

            if (items != null)
            {
                OnItemsChanged(items.Select(i => i.Id).ToList());
            }

            return(true);
        }
        private TcpMessage HandleDeleteItem(TcpConnectedHost host, TcpMessage message)
        {
            if (!message.HasType(TcpRequestType.DeleteItem))
            {
                return(null);
            }

            if (!IsAuthenticated(host))
            {
                throw AuthException.NotLoggedIn();
            }

            var request = new ObjectTcpMessage <DbItem>(message);

            var item = request.Item;

            if (item != null)
            {
                _dataService.Delete(item);
            }

            return(new TcpMessage(TcpRequestType.Ok));
        }