Exemple #1
0
        public async Task <bool> SetDisplayName(string Id, string newDisplayName)
        {
            // make sure the user isn't trying to inject via the id
            if (Helpers.FullVerifyGuid(ref Id, logger) is false)
            {
                return(false);
            }

            // clean any possible sql
            Helpers.CleanInputBasic(ref newDisplayName);

            if (string.IsNullOrEmpty(newDisplayName))
            {
                return(false);
            }

            if (string.IsNullOrWhiteSpace(newDisplayName))
            {
                return(false);
            }

            // make sure the display name has no leading or trailing whitespace
            newDisplayName = newDisplayName.Trim();

            // get the old display name to check to see if we need to update or set the name
            IFriendModel myFriendInfo = await friendHandler.GetFriend(Id);

            bool alreadyAdded = myFriendInfo != default;

            string storedProcedure;

            dynamic parameters = new { Id, DisplayName = newDisplayName };

            if (alreadyAdded)
            {
                logger.LogInformation("Changing displayname from {OldDisplayName} to {NewDisplayName} for {Id}", myFriendInfo.DisplayName, newDisplayName, Id);
                storedProcedure = ChangeDisplayNameProcedure;
            }
            else
            {
                logger.LogInformation("Setting new display name for {Id} Name: {NewDisplayName}", Id, newDisplayName);
                storedProcedure = SetDisplayNameProcedure;
            }

            try
            {
                if (myFriendInfo != null)
                {
                    // check to make sure the unique id for this person isn't taken for the next username they are switching to
                    if (await UniqueIdentifierAvailable(newDisplayName, myFriendInfo.UniqueIdentifier) is false)
                    {
                        await SetRandomUniqueIdentifier(Id, newDisplayName);
                    }
                }
                else
                {
                    await SetRandomUniqueIdentifier(Id, newDisplayName);
                }

                // change or set the name
                await db.ExecuteProcedure <dynamic, dynamic>(storedProcedure, parameters);

                // update the name in cache
                await Cache.UpdateOrCache(GetDisplayNameProcedure, new { Id }, newDisplayName);

                return(true);
            }
            catch (Exception e)
            {
                logger.LogError("Failed to change for {Id} Name: {NewDisplayName} null unique Id retrieved Error: {Error}", Id, newDisplayName, e);

                return(false);
            }
        }