Ejemplo n.º 1
0
        public async Task <OpResult> UpsertForumUser(ForumUserModel forumUser, Guid forumUserGuid, bool isForumAdmin, bool importFlag = false)
        {
            var            retvalue          = new OpResult();
            ForumUserModel existingForumUser = null;

            if (forumUser == null)
            {
                retvalue.LogError(USER_ERROR_DATA_MUST_BE_PROVIDED);
                return(retvalue);
            }
            if (!importFlag && forumUser.Guid != Guid.Empty)
            {
                existingForumUser = await _forumDal.GetForumUserById(forumUser.Guid, null);

                if (existingForumUser == null)
                {
                    retvalue.LogError(USER_ERROR_INVALID_RECORD);
                }
                else
                {
                    forumUser.CreatedUTC    = existingForumUser.CreatedUTC;
                    forumUser.ModifiedUTC   = existingForumUser.ModifiedUTC;
                    forumUser.LastIPAddress = existingForumUser.LastIPAddress;
                    forumUser.LastLogon     = existingForumUser.LastLogon;
                    if (!isForumAdmin)
                    {
                        forumUser.AdminFlag = existingForumUser.AdminFlag;
                        forumUser.BanFlag   = existingForumUser.BanFlag;
                        forumUser.UserId    = existingForumUser.UserId;
                        if (forumUser.Guid != forumUserGuid)
                        {
                            retvalue.LogError(USER_ERROR_NOT_AUTHORIZED);
                        }
                    }
                }
            }
            if (string.IsNullOrWhiteSpace(forumUser.UserId))
            {
                retvalue.LogError(USER_ERROR_USER_ID_IS_REQUIRED);
            }
            if (string.IsNullOrWhiteSpace(forumUser.Email))
            {
                retvalue.LogError(USER_ERROR_EMAIL_IS_REQUIRED);
            }
            if (forumUser.Guid == Guid.Empty && string.IsNullOrWhiteSpace(forumUser.NewPassword.Value))
            {
                retvalue.LogError(USER_ERROR_PASSWORD_IS_REQUIRED_FOR_NEW_USERS);
            }
            if (string.IsNullOrEmpty(forumUser.Comment))
            {
                retvalue.LogError(USER_ERROR_COMMENT_IS_REQUIRED);
            }
            if (retvalue.Successful)
            {
                if (importFlag || forumUser.Guid == Guid.Empty)
                {
                    if (!importFlag)
                    {
                        forumUser.Password = Security.Encrypt(forumUser.NewPassword.Value);
                    }
                    await _forumDal.InsertForumUser(forumUser, importFlag);
                }
                else
                {
                    if (forumUser.NewPassword.Reset)
                    {
                        forumUser.Password = Security.Encrypt(string.Empty);
                    }
                    else if (!string.IsNullOrEmpty(forumUser.NewPassword.Value))
                    {
                        forumUser.Password = Security.Encrypt(forumUser.NewPassword.Value);
                    }
                    else
                    {
                        forumUser.Password = existingForumUser?.Password ?? string.Empty;
                    }
                    await _forumDal.UpdateForumUser(forumUser);
                }
            }
            return(retvalue);
        }