Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
0 votes
1.2k views
in Q2A Core by

Does anybody know how ro check in custom theme if the actual post is by the actual logged in user ?

I have:

 

$userid=qa_get_logged_in_userid();
$cookieid=qa_cookie_get();
 
$rules['isbyuser']=qa_post_is_by_user($post, $userid, $cookieid);
 
but I do not have $post .
 
Besides i am sure that there is an easier way...
 
Any help is welcome

3 Answers

0 votes
by

OK, that worked:

 

 

$mypostuserid = qa_db_query_sub("SELECT userid FROM ^posts WHERE type = 'Q' and postid = $mypostid");
$myfinalpostuserid = qa_db_read_one_value($mypostuserid,true);
 
$myfinalpostuserid is the userid of the post author and i can compare it to the logged in userid
0 votes
by

Check $this->content['q_view']['raw']['userid']

by
0 votes
by
edited by

update: use the proposed code of gidgreen (shorter and better):

$isbyuser = qa_post_is_by_user($this->content['q_view']['raw'], qa_get_logged_in_userid(), qa_cookie_get());

if($isbyuser) { ...  }

 

--

if you can rely only on cookies, you can use that:

// works for anonymous
$cookieid_poster = $this->content['q_view']['raw']['cookieid'];
$cookieid_user = qa_cookie_get();

// works for logged in users
$useridReader = qa_get_logged_in_userid();
$useridPoster = $this->content['q_view']['raw']['userid'];

if( ( $cookieid_poster == $cookieid_user ) || ($useridReader == $useridPoster) ) {
   ...
}

by
You're part of the way. The right answer is:

qa_post_is_by_user($this->content['q_view']['raw'], qa_get_logged_in_userid(), qa_cookie_get());

See the source for qa_post_is_by_user(...) for the logic.
by
doing the first implementation, I tried that, but could not understand why I have to throw the entire raw-post. So I skipped this and did the code above.

I actually don't understand 100% how the code of qa_post_is_by_user() is working. If I see right, it is doing the same that I am doing in the code above?

if (@$post['userid'] || $userid)
    return @$post['userid']==$userid;
elseif (@$post['cookieid'])
    return strcmp($post['cookieid'], $cookieid)==0;
return false;
by
@gidgreen: Yes, I am using your method now as it detects better. Thanks for that!
...