コード例 #1
0
 private bool ValidateEditRequest(UserSettingsEditRequest req)
 {
     if (req.UserId <= 0)
     {
         return(false);
     }
     if (req.Key == null || req.Key.Equals(""))
     {
         return(false);
     }
     if (req.Value == null || req.Value.Equals(""))
     {
         return(false);
     }
     if (req.AuthToken == null || req.AuthToken.Equals("") || req.AuthToken.Equals("x''"))
     {
         return(false);
     }
     return(!(req.LoginToken == null || req.LoginToken.Equals("") || req.LoginToken.Equals("x''")));
 }
コード例 #2
0
        /// <summary>
        /// Request editing a user's setting's value. Documention is found in the Web API Enumeration file
        /// in the User/Settings tab, starting at row 30
        /// </summary>
        /// <param name="ctx">The HttpListenerContext to respond to</param>
        private void HandlePatchRequest(HttpListenerContext ctx)
        {
            try
            {
                #region Input Validation
                if (!ctx.Request.HasEntityBody)
                {
                    WriteBodyResponse(ctx, 400, "No Body", "Request lacked a body");
                    return;
                }
                UserSettingsEditRequest req = JsonDataObjectUtil <UserSettingsEditRequest> .ParseObject(ctx);

                if (req == null)
                {
                    WriteBodyResponse(ctx, 400, "Incorrect Format", "Request was in the wrong format");
                    return;
                }
                if (!ValidateEditRequest(req))
                {
                    WriteBodyResponse(ctx, 400, "Incorrect Format", "Not all fields of the request were filled");
                    return;
                }
                #endregion

                MySqlDataManipulator connection = new MySqlDataManipulator();
                using (connection)
                {
                    bool res = connection.Connect(MySqlDataManipulator.GlobalConfiguration.GetConnectionString());
                    if (!res)
                    {
                        WriteBodyResponse(ctx, 500, "Unexpected ServerError", "Connection to database failed");
                        return;
                    }
                    #region User Validation
                    var user = connection.GetUserById(req.UserId);
                    if (user == null)
                    {
                        WriteBodyResponse(ctx, 404, "Not Found", "User was not found on the server");
                        return;
                    }
                    if (!UserVerificationUtil.LoginTokenValid(user, req.LoginToken))
                    {
                        WriteBodyResponse(ctx, 401, "Unauthorized", "Login Token was expired or incorrect");
                        return;
                    }
                    if (!UserVerificationUtil.AuthTokenValid(user, req.AuthToken))
                    {
                        WriteBodyResponse(ctx, 401, "Unauthorized", "Auth Token was expired or incorrect");
                        return;
                    }
                    #endregion

                    #region Action Handling
                    if (!user.UpdateSettings(req.Key, req.Value))
                    {
                        WriteBodyResponse(ctx, 404, "NotFound", "Setting with key " + req.Key + " was not found.");
                        return;
                    }
                    if (!connection.UpdateUsersSettings(user))
                    {
                        WriteBodyResponse(ctx, 500, "Unexpected Server Error", "Exception: " + connection.LastException.Message);
                        return;
                    }
                    WriteBodylessResponse(ctx, 200, "OK");
                    #endregion
                }
            }
            catch (HttpListenerException)
            {
                //HttpListeners dispose themselves when an exception occurs, so we can do no more.
            }
            catch (Exception e)
            {
                WriteBodyResponse(ctx, 500, "Internal Server Error", e.Message);
            }
        }