Beispiel #1
0
        public int ValidateUpdateAccount(DtoAccount dto)
        {
            var id = dto.id;

            if (id == 17579)
            {
            }

            // validate first, starting from fastest checks, and only after the validation create an account
            if (id < 0 || id >= MAX_ACCOUNTS)
            {
                return(404);
            }

            // phone (if provided) contains area code
            if (dto.flags.HasFlag(DtoFlags.Phone) && dto.phone != null)
            {
                var openBrace  = dto.phone.IndexOf('(');
                var closeBrace = dto.phone.IndexOf(')');
                if (openBrace < 0 || closeBrace != openBrace + 4)
                {
                    return(400);
                }
            }

            // joined within range
            if (dto.flags.HasFlag(DtoFlags.Joined) && (Utils.TimestampToDate(dto.joined) < MinJoined || Utils.TimestampToDate(dto.joined) > MaxJoined))
            {
                return(400);
            }

            // premium
            if (dto.flags.HasFlag(DtoFlags.Premium))
            {
                if (dto.premium.start > 0 && Utils.TimestampToDate(dto.premium.start) < MinPremium ||
                    dto.premium.finish > 0 && Utils.TimestampToDate(dto.premium.finish) < MinPremium)
                {
                    return(400);
                }
            }

            // the rest requires locking
            bool lockTaken = false;

            try
            {
                updateLock.Enter(ref lockTaken);

                // check if the account exists
                if (!All[id])
                {
                    return(404);
                }

                // likes
                if (dto.flags.HasFlag(DtoFlags.Likes))
                {
                    if (!verifyLikes(dto.likes))
                    {
                        return(400);
                    }
                }

                var acct = Accounts[id];

                // update email and domain
                if (dto.flags.HasFlag(DtoFlags.Email))
                {
                    if (dto.email.IsEmpty || dto.email.Length > 100)
                    {
                        return(400);
                    }

                    if (!bufferFromEmail(dto.email, out var intEmail))
                    {
                        return(400);
                    }

                    // check for duplicates
                    if (Emails.Contains(intEmail) &&
                        !ByteArrayComparer.Instance.Equals(acct.Email, intEmail))
                    {
                        return(400); // such email exists and it's not ours
                    }
                    // unregister old email
                    Domains[acct.GetDomainIdx()].Exclude(id);
                    Emails.Remove(acct.Email);

                    // store and register new email
                    acct.Email = intEmail;
                    Emails.Add(acct.Email);
                    Domains[acct.GetDomainIdx()].Include(id);
                }

                // store new account info
                Accounts[id] = acct;
            }
            finally
            {
                if (lockTaken)
                {
                    updateLock.Exit();
                }
            }

            return(202);
        }