1313use OCP \AppFramework \Controller ;
1414use OCP \AppFramework \Http \TemplateResponse ;
1515use OCP \IConfig ;
16+ use OCP \IL10N ;
1617use OCP \IRequest ;
1718
1819class BaseOidcController extends Controller {
1920
2021 public function __construct (
2122 IRequest $ request ,
2223 private IConfig $ config ,
24+ private IL10N $ l ,
2325 ) {
2426 parent ::__construct (Application::APP_ID , $ request );
2527 }
@@ -39,12 +41,69 @@ protected function isDebugModeEnabled(): bool {
3941 * @return TemplateResponse
4042 */
4143 protected function buildErrorTemplateResponse (string $ message , int $ statusCode , array $ throttleMetadata = [], ?bool $ throttle = null ): TemplateResponse {
44+ $ params = [
45+ 'message ' => $ message ,
46+ 'title ' => $ this ->l ->t ('Error ' ),
47+ ];
48+ return $ this ->buildFailureTemplateResponse ($ params , $ statusCode , $ throttleMetadata , $ throttle );
49+ }
50+
51+ /**
52+ * @param string $message
53+ * @param int $statusCode
54+ * @param array $throttleMetadata
55+ * @param bool|null $throttle
56+ * @return TemplateResponse
57+ */
58+ protected function build403TemplateResponse (string $ message , int $ statusCode , array $ throttleMetadata = [], ?bool $ throttle = null ): TemplateResponse {
59+ $ params = [
60+ 'message ' => $ message ,
61+ 'title ' => $ this ->l ->t ('Access forbidden ' ),
62+ ];
63+ return $ this ->buildFailureTemplateResponse ($ params , $ statusCode , $ throttleMetadata , $ throttle );
64+ }
65+
66+ /**
67+ * @param array $params
68+ * @param int $statusCode
69+ * @param array $throttleMetadata
70+ * @param bool|null $throttle
71+ * @return TemplateResponse
72+ */
73+ protected function buildFailureTemplateResponse (
74+ array $ params , int $ statusCode , array $ throttleMetadata = [], ?bool $ throttle = null ,
75+ ): TemplateResponse {
76+ $ response = new TemplateResponse (
77+ Application::APP_ID ,
78+ 'error ' ,
79+ $ params ,
80+ TemplateResponse::RENDER_AS_ERROR
81+ );
82+ $ response ->setStatus ($ statusCode );
83+ // if not specified, throttle if debug mode is off
84+ if (($ throttle === null && !$ this ->isDebugModeEnabled ()) || $ throttle ) {
85+ $ response ->throttle ($ throttleMetadata );
86+ }
87+ return $ response ;
88+ }
89+
90+ // TODO: use the following methods only when 32 is the min supported version
91+ // as it includes the "back to NC" button
92+
93+ /**
94+ * @param string $message
95+ * @param int $statusCode
96+ * @param array $throttleMetadata
97+ * @param bool|null $throttle
98+ * @return TemplateResponse
99+ */
100+ protected function buildCoreErrorTemplateResponse (string $ message , int $ statusCode , array $ throttleMetadata = [], ?bool $ throttle = null ): TemplateResponse {
42101 $ params = [
43102 'errors ' => [
44103 ['error ' => $ message ],
45104 ],
46105 ];
47- return $ this ->buildFailureTemplateResponse ('' , 'error ' , $ params , $ statusCode , $ throttleMetadata , $ throttle );
106+ return $ this ->buildCoreFailureTemplateResponse ('' , 'error ' , $ params , $ statusCode , $ throttleMetadata , $ throttle );
48107 }
49108
50109 /**
@@ -54,9 +113,9 @@ protected function buildErrorTemplateResponse(string $message, int $statusCode,
54113 * @param bool|null $throttle
55114 * @return TemplateResponse
56115 */
57- protected function build403TemplateResponse (string $ message , int $ statusCode , array $ throttleMetadata = [], ?bool $ throttle = null ): TemplateResponse {
116+ protected function buildCore403TemplateResponse (string $ message , int $ statusCode , array $ throttleMetadata = [], ?bool $ throttle = null ): TemplateResponse {
58117 $ params = ['message ' => $ message ];
59- return $ this ->buildFailureTemplateResponse ('core ' , '403 ' , $ params , $ statusCode , $ throttleMetadata , $ throttle );
118+ return $ this ->buildCoreFailureTemplateResponse ('core ' , '403 ' , $ params , $ statusCode , $ throttleMetadata , $ throttle );
60119 }
61120
62121 /**
@@ -68,7 +127,7 @@ protected function build403TemplateResponse(string $message, int $statusCode, ar
68127 * @param bool|null $throttle
69128 * @return TemplateResponse
70129 */
71- protected function buildFailureTemplateResponse (string $ appName , string $ templateName , array $ params , int $ statusCode ,
130+ protected function buildCoreFailureTemplateResponse (string $ appName , string $ templateName , array $ params , int $ statusCode ,
72131 array $ throttleMetadata = [], ?bool $ throttle = null ): TemplateResponse {
73132 $ response = new TemplateResponse (
74133 $ appName ,
0 commit comments