Ejemplo n.º 1
0
        private void InternalWrite(string message, RequestResponseType requestResponse, MessageFilterType msgFilterType, MessageFilterLevel msgFilterLevel, string symbolOrMarket, LogEntryType logType, ConsoleColor color)
        {
            string messageToLog = string.Empty;
            //string logEntryTypeString = String.Empty;

            //if (logType == LogEntryType.Error || logType == LogEntryType.Fatal)
            //    logEntryTypeString = "ERROR";
            //else if (logType == LogEntryType.Warning)
            //    logEntryTypeString = "WARNING";

            StringBuilder sb = new StringBuilder();

            sb.Append(DateTime.Now.ToString("HH:mm:ss ffff"));
            //sb.Append(" : [");
            sb.Append(" : ");
            //sb.Append(category);

            //if (!String.IsNullOrEmpty(logEntryTypeString))
            //{
            //    sb.Append(":");
            //    sb.Append(logEntryTypeString);
            //}

            //sb.Append("] ");
            sb.Append(message);
            sb.Append(" : ");
            sb.Append(requestResponse);
            sb.Append(" : ");
            sb.Append(msgFilterType);
            sb.Append(" : ");
            sb.Append(msgFilterLevel);
            sb.Append(" : ");
            sb.Append(symbolOrMarket);
            sb.Append(" : ");
            sb.Append(logType);
            sb.Append("\n");
            messageToLog = sb.ToString();

            if (color != ConsoleColor.Normal)
            {
                readWriteLock.AcquireWriterLock(1000000);
                try
                {
                    ConsoleColor oldColor = Color;
                    Color = color;
                    Console.Write(messageToLog);
                    Color = oldColor;
                }
                finally
                {
                    readWriteLock.ReleaseWriterLock();
                }
            }
            else
            {
                Console.WriteLine(messageToLog);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// sending a message response via socket communication
        /// </summary>
        /// <param name="type"> type of the response </param>
        /// <param name="result"> result data being sent to with the response </param>
        /// <param name="clientSocket"> the Client Socket </param>
        public void SendResponse(RequestResponseType type, Result result, Socket clientSocket)
        {
            Response response = new Response {
                Type = type, Result = result
            };
            string responseString = JsonConvert.SerializeObject(response, jsonSettings);

            server.Send(responseString, clientSocket);
        }
Ejemplo n.º 3
0
        public void Write(string message, MessageFilterType filterType, RequestResponseType requestResponse, MessageFilterLevel filterLevel, string symbolOrMarket, LogEntryType logType, ConsoleColor textColor)
        {
            if (!isConsoleVisible)
            {
                return;
            }

            if (HasFilterSet)
            {
                InternalWrite(message, requestResponse, filterType, filterLevel, symbolOrMarket, logType, textColor);
            }
            else
            {
                InternalWrite(message, RequestResponseType.All, MessageFilterType.All, MessageFilterLevel.None, null, LogEntryType.Info, textColor);
            }
        }
Ejemplo n.º 4
0
        void SerializeResponse(HttpListenerResponse response, RequestResponseType type, object ob)
        {
            string serOb = null;

            if (type == RequestResponseType.Json)
            {
                response.ContentType = "application/json";
                serOb = JsonConvert.SerializeObject(ob);
            }
            else
            {
                response.ContentType = "application/xml";
            }

            using (var writer = new System.IO.StreamWriter(response.OutputStream))
                writer.Write(serOb);
        }
Ejemplo n.º 5
0
        public void Write(string message, RequestResponseType requestResponse, ConsoleColor textColor)
        {
            if (!isConsoleVisible)
            {
                return;
            }

            if (HasFilterSet)
            {
                //if (ApplyFilter(message, category, LogEntryType.Debug))
                //{
                InternalWrite(message, requestResponse, MessageFilterType.All, MessageFilterLevel.None, null, LogEntryType.Info, textColor);
                // }
            }
            else
            {
                InternalWrite(message, RequestResponseType.All, MessageFilterType.All, MessageFilterLevel.None, null, LogEntryType.Info, textColor);
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// processing the response via converting into a json object, depending on the type it gets different changes
        /// </summary>
        /// <param name="data">sensible data for the functionality of the application</param>
        public static void ProcessResponse(string data)
        {
            Response response = JsonConvert.DeserializeObject <Response>(data, jsonSettings);

            if (response != null)
            {
                Log?.Invoke($"Received Response of Type: {response.Type}");

                RequestResponseType type = response.Type;
                Result result            = response.Result;

                if (type == RequestResponseType.Register ||
                    type == RequestResponseType.Login)
                {
                    if (result.Type == ResultType.Error)
                    {
                        if (result.ErrorCode == 100)
                        {
                            Log?.Invoke("User not found");
                            return;
                        }
                    }

                    MainModel.SessionToken = response.Result.SessionToken;
                }
                else if (type == RequestResponseType.Logout)
                {
                }
                else if (type == RequestResponseType.CreateEvent)
                {
                }
                else if (type == RequestResponseType.EditEvent)
                {
                }
                else if (type == RequestResponseType.DeleteEvent)
                {
                }
                else if (type == RequestResponseType.FindEvent)
                {
                }
                else if (type == RequestResponseType.InviteToEvent)
                {
                }
                else if (type == RequestResponseType.AcceptEvent)
                {
                }
                else if (type == RequestResponseType.LeaveEvent)
                {
                }
                else if (type == RequestResponseType.AddComment)
                {
                }
                else if (type == RequestResponseType.DeleteComment)
                {
                }
                else if (type == RequestResponseType.FindCurrentUser)
                {
                    if (result.Type == ResultType.Error)
                    {
                        if (result.ErrorCode == 100)
                        {
                            Log?.Invoke("User not found");
                            return;
                        }
                    }

                    MainModel.User = response.Result.FoundUser;
                }
                else if (type == RequestResponseType.FindUser)
                {
                }
                else if (type == RequestResponseType.EditUser)
                {
                }
                else if (type == RequestResponseType.DeleteUser)
                {
                }
            }
        }
Ejemplo n.º 7
0
        public static string SharedClientKey = "[Config_SharedClientKey]"; //<-- Used to verify calls for WCF service methods NOT tied to a user login.

        public static RequestResponseType ValidateRequest(string requesterId, RequesterType requsterType, out string requesterName, out string requesterEmail, string lowestRoleRequirementPlatform = null, string lowestRoleRequirementAccount = null, bool requiresAccountOwner = false, bool ignoreAccountActiveState = false)
        {
            var response = new RequestResponseType();

            requesterName  = string.Empty;
            requesterEmail = string.Empty;

            #region validate request

            if (string.IsNullOrEmpty(requesterId))
            {
                return(new RequestResponseType {
                    isApproved = false, requestMessage = "A valid RequesterID must be used with this action"
                });
            }

            /*
             * if (requsterType == null)
             * {
             *  return new RequestResponseType { isApproved = false, requestMessage = "A RequesterType must be used with this action" };
             * }*/

            #endregion



            switch (requsterType)
            {
            //Request is exempt from further validation
            case RequesterType.Exempt:
            {
                response.isApproved     = true;
                response.requestMessage = "This request is exempt from validation.";

                #region get the requester info for the out object for logging purposes in WCF services

                try
                {
                    var requestUser = AccountUserManager.GetUser(requesterId);

                    if (requestUser != null)
                    {
                        requesterName  = requestUser.FirstName;
                        requesterEmail = requestUser.Email;
                    }
                    else
                    {
                        var plaformUser = PlatformUserManager.GetUser(requesterId);
                        requesterName  = plaformUser.FirstName;
                        requesterEmail = plaformUser.Email;
                    }
                }
                catch
                {
                    var requestUser = PlatformUserManager.GetUser(requesterId);
                    requesterName  = requestUser.FirstName;
                    requesterEmail = requestUser.Email;
                }

                #endregion

                break;
            }

            //validate request for a PlatformUser:
            case RequesterType.PlatformUser:
            {
                if (lowestRoleRequirementPlatform != null)
                {
                    //userRole = PlatformUserManager.GetUserRole(requesterId);

                    var platformUser = PlatformUserManager.GetUser(requesterId);

                    requesterName  = platformUser.FirstName;
                    requesterEmail = platformUser.Email;

                    //userRole = AccountUserManager.GetUserRole(requesterId);

                    //Check requester Active state:
                    if (!platformUser.Active)
                    {
                        response.isApproved     = false;
                        response.requestMessage = "You must be an active platform user to make this request.";

                        //immediatley return the failed result
                        return(response);
                    }


                    //Check requester role:
                    response.isApproved = Internal.RoleChecker.IsRoleAllowed(requsterType, platformUser.Role, lowestRoleRequirementPlatform);

                    if (response.isApproved)
                    {
                        response.requestMessage = "This request is valid.";
                    }
                    else
                    {
                        response.requestMessage = "This request is not valid for this platform user role.";
                    }
                }
                else
                {
                    response.isApproved     = false;
                    response.requestMessage = "This request is not valid for platform users";
                }

                break;
            }


            //Validate request(s) for an AccountUser:
            case RequesterType.AccountUser:
            {
                var accountUser = AccountUserManager.GetUser(requesterId);

                requesterName  = accountUser.FirstName;
                requesterEmail = accountUser.Email;

                var account = AccountManager.GetAccount(accountUser.AccountID.ToString(), true, AccountManager.AccountIdentificationType.AccountID);

                //Ensure that the account is Active (and Active state is not ignored):
                if (!ignoreAccountActiveState && !account.Active)
                {
                    response.isApproved     = false;
                    response.requestMessage = "This account is not currently active.";
                    //Immediately return the failed result
                    return(response);
                }

                if (!account.Provisioned)
                {
                    response.isApproved     = false;
                    response.requestMessage = "This account is not yet provisioned.";
                    //Immediately return the failed result
                    return(response);
                }

                /*
                 * //Ensure that the account is Active (and Active state is not ignored):
                 * if (!ignoreAccountActiveState && !AccountManager.IsAccountActive(accountUser.AccountID.ToString()))
                 * {
                 *  response.isApproved = false;
                 *  response.requestMessage = "This account is not currently active.";
                 *  //Immediately return the failed result
                 *  return response;
                 * }
                 *
                 */

                if (requiresAccountOwner)
                {
                    //Check if the user is an account owner
                    if (accountUser.AccountOwner)
                    {
                        response.isApproved     = true;
                        response.requestMessage = "This request is valid.";
                    }
                    else
                    {
                        response.isApproved     = false;
                        response.requestMessage = "Only account owners can make this request or update.";

                        //Immediately return the failed result
                        return(response);
                    }
                }
                else if (lowestRoleRequirementAccount != null)
                {
                    //Check requester Active state:
                    if (!accountUser.Active)
                    {
                        response.isApproved     = false;
                        response.requestMessage = "You must be an active account user to make this request.";

                        //Immediately return the failed result
                        return(response);
                    }

                    //Check requester role:
                    response.isApproved = Internal.RoleChecker.IsRoleAllowed(requsterType, accountUser.Role, lowestRoleRequirementAccount);

                    if (response.isApproved)
                    {
                        response.requestMessage = "This request is valid.";
                    }
                    else
                    {
                        response.requestMessage = "This request is not valid for this account user role.";

                        //immediatly return the failed result
                        return(response);
                    }
                }
                else
                {
                    response.isApproved     = false;
                    response.requestMessage = "This request is not valid for account users";

                    //immediatly return the failed result
                    return(response);
                }

                break;
            }


            default:
            {
                response.isApproved     = false;
                response.requestMessage = "Cannot validate this request with the parameters given.";
                break;
            }
            }


            return(response);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// receiving and converting a request via json and sending a response
        /// </summary>
        /// <param name="data"> a string with sensible data about the application </param>
        /// <param name="clientSocket"> the client socket  </param>
        private void ProcessRequest(string data, Socket clientSocket)
        {
            Request request = JsonConvert.DeserializeObject <Request>(data, jsonSettings);

            if (request != null)
            {
                Log($"Received Response of Type: {request.Type}");

                RequestResponseType type = request.Type;
                Result result;

                if (type == RequestResponseType.Register)
                {
                    nudel = new NudelService();

                    result = nudel.Register(request.User);

                    SendResponse(type, result, clientSocket);
                }
                else if (type == RequestResponseType.Login)
                {
                    nudel = new NudelService();

                    result = nudel.Login(request.User);

                    SendResponse(type, result, clientSocket);
                }
                else if (type == RequestResponseType.Logout)
                {
                    result = nudel.Logout(request.SessionToken);

                    SendResponse(type, result, clientSocket);
                }
                else if (type == RequestResponseType.CreateEvent)
                {
                    nudel = new NudelService(request.SessionToken);

                    result = nudel.CreateEvent(request.Event);

                    SendResponse(type, result, clientSocket);
                }
                else if (type == RequestResponseType.EditEvent)
                {
                    nudel = new NudelService(request.SessionToken);

                    result = nudel.EditEvent(request.Event);

                    SendResponse(type, result, clientSocket);
                }
                else if (type == RequestResponseType.DeleteEvent)
                {
                    nudel = new NudelService(request.SessionToken);

                    result = nudel.DeleteEvent(request.Event);

                    SendResponse(type, result, clientSocket);
                }
                else if (type == RequestResponseType.FindEvent)
                {
                    nudel = new NudelService(request.SessionToken);

                    result = nudel.FindEvent(request.Event.ID);

                    SendResponse(type, result, clientSocket);
                }
                else if (type == RequestResponseType.InviteToEvent)
                {
                    nudel = new NudelService(request.SessionToken);

                    result = nudel.InviteToEvent(request.Event, request.User);

                    SendResponse(type, result, clientSocket);
                }
                else if (type == RequestResponseType.AcceptEvent)
                {
                    nudel = new NudelService(request.SessionToken);

                    result = nudel.AcceptEvent(request.Event);

                    SendResponse(type, result, clientSocket);
                }
                else if (type == RequestResponseType.LeaveEvent)
                {
                    nudel = new NudelService(request.SessionToken);

                    result = nudel.CreateEvent(request.Event);

                    SendResponse(type, result, clientSocket);
                }
                else if (type == RequestResponseType.AddComment)
                {
                    nudel = new NudelService(request.SessionToken);

                    result = nudel.AddComment(request.Event, request.Comment);

                    SendResponse(type, result, clientSocket);
                }
                else if (type == RequestResponseType.DeleteComment)
                {
                    nudel = new NudelService(request.SessionToken);

                    result = nudel.DeleteComment(request.Event, request.Comment);

                    SendResponse(type, result, clientSocket);
                }
                else if (type == RequestResponseType.FindCurrentUser)
                {
                    nudel = new NudelService(request.SessionToken);

                    result = nudel.FindCurrentUser();

                    SendResponse(type, result, clientSocket);
                }
                else if (type == RequestResponseType.FindUser)
                {
                    nudel = new NudelService(request.SessionToken);

                    result = nudel.FindUser(request.User.ID);

                    SendResponse(type, result, clientSocket);
                }
                else if (type == RequestResponseType.EditUser)
                {
                    nudel = new NudelService(request.SessionToken);

                    result = nudel.EditUser(request.User);

                    SendResponse(type, result, clientSocket);
                }
                else if (type == RequestResponseType.DeleteUser)
                {
                    nudel = new NudelService(request.SessionToken);

                    result = nudel.DeleteUser(request.User);

                    SendResponse(type, result, clientSocket);
                }
            }
        }