2121 * See the License for the specific language governing permissions and
2222 * limitations under the License.
2323/*======================================================*/
24- require_once __DIR__ .'/../../../web_portal/components/Get_User_Principle.php ' ;
25- require_once __DIR__ .'/../utils.php ' ;
26- require_once __DIR__ .'/../../../../lib/Gocdb_Services/Factory.php ' ;
24+ require_once __DIR__ . '/../../../web_portal/components/Get_User_Principle.php ' ;
25+ require_once __DIR__ . '/../utils.php ' ;
26+ require_once __DIR__ . '/../../../../lib/Gocdb_Services/Factory.php ' ;
27+
28+ use Exception ;
2729
2830/**
29- * Controller to edit authentication entity request
31+ * Controller to either edit authentication entity request or renewal request.
32+ *
3033 * @global array $_POST only set if the browser has POSTed data
3134 * @return null
3235 */
33- function edit_entity () {
34- $ dn = Get_User_Principle ();
35- $ user = \Factory::getUserService ()->getUserByPrinciple ($ dn );
36+ function edit_entity ()
37+ {
38+ list ($ user , $ authEnt , $ site , $ serv ) = initialize ();
39+
40+ if ($ _POST ) {
41+ submit ($ user , $ authEnt , $ site , $ serv );
42+ } else {
43+ draw ($ user , $ authEnt , $ site );
44+ }
45+ }
3646
37- //Check the portal is not in read only mode, returns exception if it is and user is not an admin
47+ function initialize ()
48+ {
49+ $ identifier = Get_User_Principle ();
50+ $ user = \Factory::getUserService ()->getUserByPrinciple ($ identifier );
51+
52+ /**
53+ * Check the portal is not in read only mode,
54+ * returns exception if it is and user is not an admin.
55+ */
3856 checkPortalIsNotReadOnlyOrUserIsAdmin ($ user );
3957
40- if (!isset ($ _REQUEST ['authentityid ' ]) || !is_numeric ($ _REQUEST ['authentityid ' ]) ) {
58+ if (!isset ($ _REQUEST ['authentityid ' ]) || !is_numeric ($ _REQUEST ['authentityid ' ])) {
4159 throw new Exception ("A authentication entity id must be specified in the url " );
4260 }
4361
@@ -47,48 +65,80 @@ function edit_entity() {
4765
4866 // Validate the user has permission to edit properties
4967 if (!$ serv ->userCanEditSite ($ user , $ site )) {
50- throw new \Exception ("Permission denied: a site role is required to edit authentication entities at " . $ site ->getShortName ());
68+ throw new \Exception (
69+ "Permission denied: a site role is required to edit authentication entities at " .
70+ $ site ->getShortName ()
71+ );
5172 }
5273
53- if ($ _POST ) { // If we receive a POST request it's to edit an authentication entity
54- submit ($ user , $ authEnt , $ site , $ serv );
55- } else { // If there is no post data, draw the edit authentication entity form
56- draw ($ user , $ authEnt , $ site );
57- }
74+ return [$ user , $ authEnt , $ site , $ serv ];
5875}
5976
60- function draw (\User $ user = null , \APIAuthentication $ authEnt = null , \Site $ site = null ) {
61- if (is_null ($ user )){
77+ /**
78+ * Helper to draw either the edit or renewal authentication entity form.
79+ *
80+ * @param \User|null $user
81+ * @param \APIAuthentication|null $authEntity
82+ * @param \Site|null $site
83+ * @throws \Exception
84+ */
85+ function draw (
86+ \User $ user = null ,
87+ \APIAuthentication $ authEnt = null ,
88+ \Site $ site = null
89+ ) {
90+ if (is_null ($ user )) {
6291 throw new Exception ("Unregistered users can't edit authentication credentials " );
6392 }
6493
6594 $ params = array ();
6695 $ params ['site ' ] = $ site ;
6796 $ params ['authEnt ' ] = $ authEnt ;
6897 $ params ['authTypes ' ] = array ();
69- $ params ['authTypes ' ][]= 'X.509 ' ;
70- $ params ['authTypes ' ][]= 'OIDC Subject ' ;
98+ $ params ['authTypes ' ][] = 'X.509 ' ;
99+ $ params ['authTypes ' ][] = 'OIDC Subject ' ;
71100 $ params ['user ' ] = $ user ;
72101
102+ if ($ _REQUEST ['isRenewalRequest ' ]) {
103+ $ params ['isRenewalRequest ' ] = true ;
104+ }
73105 show_view ("site/edit_api_auth.php " , $ params );
74106 die ();
75107}
76108
77- function submit (\User $ user , \APIAuthentication $ authEnt , \Site $ site , org \gocdb \services \Site $ serv ) {
78- $ newValues = getAPIAuthenticationFromWeb ();
109+ /**
110+ * If this receives a POST request, it can be either to edit an API authentication entity
111+ * or to update the `$lastRenewTime` in `APIAuthentication`.
112+ *
113+ * @param \User $user
114+ * @param \APIAuthentication $authEntity
115+ * @param \Site $site
116+ * @param org\gocdb\services\Site $service
117+ */
118+ function submit (
119+ \User $ user ,
120+ \APIAuthentication $ authEnt ,
121+ \Site $ site ,
122+ org \gocdb \services \Site $ serv
123+ ) {
124+ $ params = array ();
125+
126+ if ($ _REQUEST ['isRenewalRequest ' ]) {
127+ $ newValues ['isRenewalRequest ' ] = $ params ['isRenewalRequest ' ] = true ;
128+ } else {
129+ $ newValues = getAPIAuthenticationFromWeb ();
130+ }
79131
80132 try {
81133 $ authEnt = $ serv ->editAPIAuthEntity ($ authEnt , $ user , $ newValues );
82- } catch (Exception $ e ) {
134+ } catch (Exception $ e ) {
83135 show_view ('error.php ' , $ e ->getMessage ());
84136 die ();
85137 }
86138
87- $ params = array ();
88139 $ params ['apiAuthenticationEntity ' ] = $ authEnt ;
89140 $ params ['site ' ] = $ site ;
141+
90142 show_view ("site/edited_api_auth.php " , $ params );
91143 die ();
92-
93-
94144}
0 commit comments