| @@ 21-64 (lines=44) @@ | ||
| 18 | return $app['twig']->render('credList.html.twig', array('credentials' => $credentials)); |
|
| 19 | } |
|
| 20 | ||
| 21 | public function addCredentialAction(Request $request, Application $app) |
|
| 22 | { |
|
| 23 | $credential = new Credential(); |
|
| 24 | $credentialForm = $app['form.factory']->create(new CredentialType(), $credential, ['mapped' => true, 'disable' => false]); |
|
| 25 | $credentialForm->handleRequest($request); |
|
| 26 | ||
| 27 | if ($credentialForm->isSubmitted() && $credentialForm->isValid()) { |
|
| 28 | $token = $credential->getToken(); |
|
| 29 | $name = $credential->getNameCred(); |
|
| 30 | ||
| 31 | try { |
|
| 32 | $client = new Client(); |
|
| 33 | $res = $client->get('https://api.github.com/user', [ |
|
| 34 | 'auth' => [ |
|
| 35 | 'token', |
|
| 36 | $token |
|
| 37 | ] |
|
| 38 | ]); |
|
| 39 | $status = $res->getStatusCode(); |
|
| 40 | $testName = json_decode($res->getBody()->getContents(), true); |
|
| 41 | ||
| 42 | } catch (ClientException $e) { |
|
| 43 | $status = $e->getResponse()->getStatusCode(); |
|
| 44 | } |
|
| 45 | if ($status == 200) { |
|
| 46 | if ($name == $testName['login']) { |
|
| 47 | $app['credential_repository']->save($credential); |
|
| 48 | $app['session']->getFlashBag()->add('success', 'The credential was successfully created.'); |
|
| 49 | } else { |
|
| 50 | $app['session']->getFlashBag()->add('danger', |
|
| 51 | 'This is not the user assigned at this token.'); |
|
| 52 | } |
|
| 53 | } else { |
|
| 54 | $app['session']->getFlashBag()->add('danger', |
|
| 55 | 'The token is incorect. (GitHub API error ' . $status . ')'); |
|
| 56 | } |
|
| 57 | } |
|
| 58 | ||
| 59 | return $app['twig']->render('credList_form.html.twig', array( |
|
| 60 | 'title' => 'New credential', |
|
| 61 | 'legend' => 'New credential', |
|
| 62 | 'credentialForm' => $credentialForm->createView(), |
|
| 63 | )); |
|
| 64 | } |
|
| 65 | ||
| 66 | public function editCredentialAction($id, Request $request, Application $app) |
|
| 67 | { |
|
| @@ 66-109 (lines=44) @@ | ||
| 63 | )); |
|
| 64 | } |
|
| 65 | ||
| 66 | public function editCredentialAction($id, Request $request, Application $app) |
|
| 67 | { |
|
| 68 | $credential = $app['credential_repository']->find($id); |
|
| 69 | $credentialForm = $app['form.factory']->create(new CredentialType(), $credential, ['mapped' => false, 'disable' => true]); |
|
| 70 | $credentialForm->handleRequest($request); |
|
| 71 | ||
| 72 | if ($credentialForm->isSubmitted() && $credentialForm->isValid()) { |
|
| 73 | $token = $credential->getToken(); |
|
| 74 | $name = $credential->getNameCred(); |
|
| 75 | ||
| 76 | try { |
|
| 77 | $client = new Client(); |
|
| 78 | $res = $client->get('https://api.github.com/user', [ |
|
| 79 | 'auth' => [ |
|
| 80 | 'token', |
|
| 81 | $token |
|
| 82 | ] |
|
| 83 | ]); |
|
| 84 | $status = $res->getStatusCode(); |
|
| 85 | $testName = json_decode($res->getBody()->getContents(), true); |
|
| 86 | ||
| 87 | } catch (ClientException $e) { |
|
| 88 | $status = $e->getResponse()->getStatusCode(); |
|
| 89 | } |
|
| 90 | if ($status == 200) { |
|
| 91 | if ($name == $testName['login']) { |
|
| 92 | $app['credential_repository']->save($credential); |
|
| 93 | $app['session']->getFlashBag()->add('success', 'The credential was successfully updated.'); |
|
| 94 | } else { |
|
| 95 | $app['session']->getFlashBag()->add('danger', |
|
| 96 | 'This is not the user assigned at this token.'); |
|
| 97 | } |
|
| 98 | } else { |
|
| 99 | $app['session']->getFlashBag()->add('danger', |
|
| 100 | 'The token is incorect. (GitHub API error ' . $status . ')'); |
|
| 101 | } |
|
| 102 | } |
|
| 103 | ||
| 104 | return $app['twig']->render('credList_form.html.twig', array( |
|
| 105 | 'title' => 'Edit credential', |
|
| 106 | 'legend' => 'Edit credential', |
|
| 107 | 'credentialForm' => $credentialForm->createView(), |
|
| 108 | )); |
|
| 109 | } |
|
| 110 | ||
| 111 | public function deleteCredentialAction($id, Application $app) |
|
| 112 | { |
|