コード例 #1
0
        /// <summary>
        /// GET request format located in the Web Api Enumeration v2
        /// under the tab Company/Requests, starting row 23
        /// </summary>
        /// <param name="ctx">HttpListenerContext to respond to</param>
        private void HandleGetRequest(HttpListenerContext ctx, CompanyRequestsGetRequest entry)
        {
            try
            {
                MySqlDataManipulator connection = new MySqlDataManipulator();
                using (connection)
                {
                    bool res = connection.Connect(MySqlDataManipulator.GlobalConfiguration.GetConnectionString());
                    if (!res)
                    {
                        WriteBodyResponse(ctx, 500, "Unexpected Server Error", "Connection to database failed");
                        return;
                    }
                    #region User Validation
                    OverallUser mappedUser = connection.GetUserById(entry.UserId);
                    if (mappedUser == null)
                    {
                        WriteBodyResponse(ctx, 404, "Not Found", "User was not found on on the server");
                        return;
                    }
                    if (!UserVerificationUtil.LoginTokenValid(mappedUser, entry.LoginToken))
                    {
                        WriteBodyResponse(ctx, 401, "Not Authorized", "Login token was incorrect.");
                        return;
                    }
                    if (!UserVerificationUtil.AuthTokenValid(mappedUser, entry.AuthToken))
                    {
                        WriteBodyResponse(ctx, 401, "Not Authorized", "Auth token was ezpired or incorrect");
                        return;
                    }
                    if ((mappedUser.AccessLevel & AccessLevelMasks.AdminMask) == 0)
                    {
                        WriteBodyResponse(ctx, 401, "Not Authorized", "User was not an administrative user");
                        return;
                    }
                    #endregion

                    #region Action Handling
                    var requests = connection.GetJoinRequests(mappedUser.Company);
                    JsonListStringConstructor returnConstructor = new JsonListStringConstructor();
                    requests.ForEach(req => returnConstructor.AddElement(WriteJoinRequestToOutput(req, connection)));
                    WriteBodyResponse(ctx, 200, "OK", returnConstructor.ToString());
                    #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);
            }
        }
コード例 #2
0
 private bool ValidateGetRequest(CompanyRequestsGetRequest req)
 {
     if (req.LoginToken == null || req.LoginToken.Equals(""))
     {
         return(false);
     }
     if (req.AuthToken == null || req.AuthToken.Equals(""))
     {
         return(false);
     }
     if (req.UserId <= 0)
     {
         return(false);
     }
     return(true);
 }
コード例 #3
0
        /// <summary>
        /// PUT request format located in the Web Api Enumeration v2
        /// under the tab Company/Requests, starting row 49
        /// </summary>
        /// <param name="ctx">HttpListenerContext to respond to</param>
        private void HandlePutRequest(HttpListenerContext ctx)
        {
            try
            {
                #region Input Validation
                if (!ctx.Request.HasEntityBody)
                {
                    WriteBodyResponse(ctx, 400, "Bad Request", "No Body");
                    return;
                }
                string requestStr = new StreamReader(ctx.Request.InputStream).ReadToEnd();
                CompanyRequestsPutRequest entry = JsonDataObjectUtil <CompanyRequestsPutRequest> .ParseObject(requestStr);

                if (!ValidatePutRequest(entry))
                {
                    CompanyRequestsGetRequest req2 = JsonDataObjectUtil <CompanyRequestsGetRequest> .ParseObject(requestStr);

                    if (req2 != null && ValidateGetRequest(req2))
                    {
                        HandleGetRequest(ctx, req2);
                        return;
                    }
                    WriteBodyResponse(ctx, 400, "Bad Request", "Incorrect Format");
                    return;
                }
                #endregion

                MySqlDataManipulator connection = new MySqlDataManipulator();
                using (connection)
                {
                    bool res = connection.Connect(MySqlDataManipulator.GlobalConfiguration.GetConnectionString());
                    if (!res)
                    {
                        WriteBodyResponse(ctx, 500, "Unexpected Server Error", "Connection to database failed");
                        return;
                    }
                    #region User Validation
                    OverallUser mappedUser = connection.GetUserById(entry.UserId);
                    if (mappedUser == null)
                    {
                        WriteBodyResponse(ctx, 404, "Not Found", "User was not found on on the server");
                        return;
                    }
                    if (!UserVerificationUtil.LoginTokenValid(mappedUser, entry.LoginToken))
                    {
                        WriteBodyResponse(ctx, 401, "Not Authorized", "Login token was incorrect.");
                        return;
                    }
                    if (!UserVerificationUtil.AuthTokenValid(mappedUser, entry.AuthToken))
                    {
                        WriteBodyResponse(ctx, 401, "Not Authorized", "Auth token was ezpired or incorrect");
                        return;
                    }
                    if ((mappedUser.AccessLevel & AccessLevelMasks.AdminMask) == 0)
                    {
                        WriteBodyResponse(ctx, 401, "Not Authorized", "User was not an administrative user");
                        return;
                    }
                    #endregion

                    #region Action Handling
                    var request = connection.GetJoinRequestById(mappedUser.Company, entry.RequestId);
                    if (request == null)
                    {
                        WriteBodyResponse(ctx, 404, "Not Found", "Request was not found on the server");
                        return;
                    }
                    if (!connection.RemoveJoinRequest(mappedUser.Company, entry.RequestId, accept: true))
                    {
                        WriteBodyResponse(ctx, 500, "Internal Server Error", "Error ocurred on the server: " + connection.LastException);
                        return;
                    }
                    mappedUser.AccessLevel = 1;
                    connection.UpdateUserAccessLevel(mappedUser);
                    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);
            }
        }