public string UpdateSettings(int userId, string email = null, string profileURL = null, string visibility = null, string token = null)
        {
            if (Request.RequestType.Equals("OPTIONS", StringComparison.InvariantCultureIgnoreCase))
            {
                return null;
            }
            try
            {
                int authUserId = -1;
                if (token != null)
                {
                    authUserId = authenticationEngine.authenticate(token);
                }
                else
                {
                    return AddErrorHeader("An authentication token must be passed in", 2);
                }

                if (authUserId != userId)
                {
                    return AddErrorHeader("Not authorized to update another user's settings", 3);
                }
                else
                {
                    User user = userManager.GetUser(userId);
                    if (user != null)
                    {
                        JsonModels.UserSettings settings = new JsonModels.UserSettings();
                        settings.email = user.email;
                        settings.firstName = user.firstName;
                        settings.lastName = user.lastName;
                        settings.profileURL = user.profileURL;
                        if (user.isPublic == 0)
                        {
                            settings.visibility = "hidden";
                        }
                        else
                        {
                            settings.visibility = "public";
                        }

                        Boolean updateTables = false;
                        if (profileURL != null && user.profileURL != profileURL)
                        {
                            string result = ValidationEngine.ValidateProfileURL(profileURL);
                            if (result == ValidationEngine.Success)
                            {
                                settings.profileURL = profileURL;
                                user.profileURL = profileURL;
                                updateTables = true;
                            }
                            else
                            {
                                return AddErrorHeader("Not a valid profile URL", 1);
                            }
                        }
                        else if (email != null && user.email != email)
                        {
                            if (userManager.CheckDuplicateEmail(email))
                            {
                                settings.email = email;
                                user.email = email;
                                updateTables = true;
                            }
                            else
                            {
                                return AddErrorHeader("Email already in use", 1);
                            }
                        }
                        else if (visibility != null && visibility != settings.visibility)
                        {
                            updateTables = true;
                            settings.visibility = visibility;
                            if (visibility == "hidden")
                            {
                                user.isPublic = 0;
                            }
                            else
                            {
                                user.isPublic = 1;
                            }
                        }
                        if (updateTables)
                        {
                            //update user now with updated fields;
                            bool success = userManager.UpdateUserSettings(user);
                            if (success)
                            {
                                return AddSuccessHeader(Serialize(settings));
                                //email user's new and old email?
                            }
                            else
                            {
                                return AddErrorHeader("Update Unsuccessful", 1);
                            }
                        }
                        else//nothing updated but still a success
                        {
                            return AddSuccessHeader(Serialize(settings));
                        }
                    }
                    else
                    {
                        return AddErrorHeader("No user found for id " + userId, 1);
                    }
                }
            }
            catch (Exception ex)
            {
                logAccessor.CreateLog(DateTime.Now, "UserController - UpdateSettingsModel", ex.StackTrace);
                return AddErrorHeader("Something went wrong while updating this  user SettingsModel", 1);
            }
        }
        public string GetUserModel(int userId, string token)
        {
            if (Request.RequestType.Equals("OPTIONS", StringComparison.InvariantCultureIgnoreCase))
            {
                return null;
            }
            try
            {
                int authUserId = -1;
                if (token != null)
                {
                    authUserId = authenticationEngine.authenticate(token);
                }
                else
                {
                    return AddErrorHeader("A token must be passed in", 2);
                }
                if (authUserId < 0)
                {
                    return AddErrorHeader("The authenticaiton token is not valid", 2);
                }

                if (authUserId != userId)
                {
                    return AddErrorHeader("User must be profile owner to retrieve the User Model", 1);
                }
                User user = userManager.GetUserWithNetworks(userId);
                if (user != null)
                {
                    NetworkManager networkManager = new NetworkManager();
                    JsonModels.UserInformationModel userInfoJson = new JsonModels.UserInformationModel();
                    JsonModels.UserSettings userSettingsJson = new JsonModels.UserSettings();
                    List<JsonModels.UserNetworkShell> userNetworksJson = new List<JsonModels.UserNetworkShell>();

                    List<Network> topLevelNetworks = new List<Network>();
                    List<Network> subNetworks = new List<Network>();
                    List<Network> groupNetworks = new List<Network>();

                    foreach (Network n in user.networks)
                    {
                        if (n != null)
                        {
                            if (n.GetType().Name.Contains("Network_TopNetwork"))
                            {
                                if (!topLevelNetworks.Contains(n))
                                {
                                    topLevelNetworks.Add(n);
                                }
                            }
                            else if (n.GetType().Name.Contains("Network_SubNetwork"))
                            {
                                if (!subNetworks.Contains(n))
                                {
                                    subNetworks.Add(n);
                                }
                            }
                            else if (n.GetType().Name.Contains("Network_Group"))
                            {
                                if (!groupNetworks.Contains(n))
                                {
                                    groupNetworks.Add(n);
                                }
                            }
                        }
                    }
                    foreach (Network n in user.adminNetworks)
                    {
                        if (n != null)
                        {
                            if (n.GetType().Name.Contains("Network_TopNetwork"))
                            {
                                if (!topLevelNetworks.Contains(n))
                                {
                                    topLevelNetworks.Add(n);
                                }
                            }
                            else if (n.GetType().Name.Contains("Network_SubNetwork"))
                            {
                                if (!subNetworks.Contains(n))
                                {
                                    subNetworks.Add(n);
                                }
                            }
                            else if (n.GetType().Name.Contains("Network_Group"))
                            {
                                if (!groupNetworks.Contains(n))
                                {
                                    groupNetworks.Add(n);
                                }
                            }
                        }
                    }

                    for (int i = 0; i < topLevelNetworks.Count; i++)
                    {
                        Network topNet = topLevelNetworks.ElementAt(i);
                        JsonModels.UserNetworkShell topNetShell = new JsonModels.UserNetworkShell();
                        topNetShell.name = topNet.name;
                        topNetShell.networkId = topNet.id;
                        topNetShell.networkURL = topNet.profileURL;
                        if (user.adminNetworks.Contains(topNet))
                        {
                            topNetShell.userAuthorization = "admin";
                        }
                        else
                        {
                            topNetShell.userAuthorization = "member";
                        }

                        for (int j = 0; j < subNetworks.Count; j++)
                        {
                            Network_SubNetwork subNet = (Network_SubNetwork)subNetworks.ElementAt(j);
                            JsonModels.UserNetworkShell subNetShell = new JsonModels.UserNetworkShell();
                            if (subNet.Network_TopNetwork.id == topNet.id)
                            {
                                subNetShell.name = subNet.name;
                                subNetShell.networkId = subNet.id;
                                subNetShell.networkURL = subNet.profileURL;
                                if (user.adminNetworks.Contains(subNet))
                                {
                                    subNetShell.userAuthorization = "admin";
                                }
                                else
                                {
                                    subNetShell.userAuthorization = "member";
                                }

                                for (int k = 0; k < groupNetworks.Count; k++)
                                {
                                    Network_Group groupNet = (Network_Group)groupNetworks.ElementAt(k);
                                    if (groupNet.Network_SubNetwork.id == subNet.id)
                                    {
                                        JsonModels.UserNetworkShell groupNetShell = new JsonModels.UserNetworkShell();
                                        groupNetShell.name = groupNet.name;
                                        groupNetShell.networkId = groupNet.id;
                                        groupNetShell.networkURL = groupNet.profileURL;
                                        if (user.adminNetworks.Contains(groupNet))
                                        {
                                            groupNetShell.userAuthorization = "admin";
                                        }
                                        else
                                        {
                                            groupNetShell.userAuthorization = "member";
                                        }
                                        if (subNetShell.subnetworks == null)
                                        {
                                            subNetShell.subnetworks = new List<JsonModels.UserNetworkShell>();
                                        }
                                        subNetShell.subnetworks.Add(groupNetShell);
                                    }
                                }
                                if (topNetShell.subnetworks == null)
                                {
                                    topNetShell.subnetworks = new List<JsonModels.UserNetworkShell>();
                                }
                                topNetShell.subnetworks.Add(subNetShell);

                            }
                        }

                        userNetworksJson.Add(topNetShell);

                    }
                    userSettingsJson.email = user.email;
                    userSettingsJson.profileURL = user.profileURL;
                    userSettingsJson.firstName = user.firstName;
                    userSettingsJson.lastName = user.lastName;
                    if(user.isPublic == 1)
                    {
                        userSettingsJson.visibility = "visible";
                    }
                    else
                    {
                        userSettingsJson.visibility = "hidden";
                    }

                    userInfoJson.networks = userNetworksJson;
                    userInfoJson.settings = userSettingsJson;

                    return AddSuccessHeader(Serialize(userInfoJson));
                }
                else
                {
                    return AddErrorHeader("User not found", 1);
                }
            }
            catch (Exception ex)
            {
                logAccessor.CreateLog(DateTime.Now, "UserController - GetUserModel", ex.StackTrace);
                return AddErrorHeader("something went wrong while retrieving this user's info", 1);
            }
        }