@@ -46,9 +46,7 @@ class QuestionController extends AbstractController
4646 */
4747 public function create (Request $ request ): JsonResponse
4848 {
49- $ user = CurrentUser::getCurrentUser ($ this ->configuration );
50-
51- if (!$ this ->isAddingQuestionsAllowed ($ user )) {
49+ if (!$ this ->isAddingQuestionsAllowed ()) {
5250 return $ this ->json (['error ' => Translation::get ('ad_msg_noauth ' )], Response::HTTP_FORBIDDEN );
5351 }
5452
@@ -69,14 +67,15 @@ public function create(Request $request): JsonResponse
6967 $ selectedCategory = isset ($ data ->category ) ? Filter::filterVar ($ data ->category , FILTER_VALIDATE_INT ) : false ;
7068 $ userQuestion = trim (strip_tags ((string ) $ data ->question ));
7169 $ save = Filter::filterVar ($ data ->save ?? 0 , FILTER_VALIDATE_INT );
70+ $ storeNow = Filter::filterVar ($ data ->store ?? 'not ' , FILTER_SANITIZE_SPECIAL_CHARS );
7271
7372 // If smart answering is disabled, save the question immediately
7473 if (false === $ this ->configuration ->get ('main.enableSmartAnswering ' )) {
7574 $ save = true ;
7675 }
7776
78- // Validate captcha
79- if (!$ this ->captchaCodeIsValid ($ request )) {
77+ // Validate captcha if we can store the question after displaying the smart answer
78+ if ($ storeNow !== ' now ' && !$ this ->captchaCodeIsValid ($ request )) {
8079 return $ this ->json (['error ' => Translation::get ('msgCaptcha ' )], Response::HTTP_BAD_REQUEST );
8180 }
8281
@@ -108,7 +107,7 @@ public function create(Request $request): JsonResponse
108107 $ faqSearch ->setCategoryId ((int ) $ selectedCategory );
109108
110109 $ faqPermission = new Permission ($ this ->configuration );
111- $ faqSearchResult = new SearchResultSet ($ user , $ faqPermission , $ this ->configuration );
110+ $ faqSearchResult = new SearchResultSet ($ this -> currentUser , $ faqPermission , $ this ->configuration );
112111
113112 $ searchResult = array_merge (...array_map (
114113 fn ($ word ) => $ faqSearch ->search ($ word , false ),
@@ -134,16 +133,16 @@ public function create(Request $request): JsonResponse
134133 }
135134 }
136135
137- private function isAddingQuestionsAllowed (CurrentUser $ user ): bool
136+ /**
137+ * @throws \Exception
138+ */
139+ private function isAddingQuestionsAllowed (): bool
138140 {
139- if (
140- !$ this ->configuration ->get ('records.allowQuestionsForGuests ' ) &&
141- !$ this ->configuration ->get ('main.enableAskQuestions ' ) &&
142- !$ user ->perm ->hasPermission ($ user ->getUserId (), PermissionType::QUESTION_ADD ->value )
143- ) {
144- return false ;
145- }
146-
147- return true ;
141+ return $ this ->configuration ->get ('records.allowQuestionsForGuests ' ) ||
142+ $ this ->configuration ->get ('main.enableAskQuestions ' ) ||
143+ $ this ->currentUser ->perm ->hasPermission (
144+ $ this ->currentUser ->getUserId (),
145+ PermissionType::QUESTION_ADD ->value
146+ );
148147 }
149148}
0 commit comments