~ubuntu-branches/ubuntu/wily/phabricator/wily

« back to all changes in this revision

Viewing changes to phabricator/src/applications/conduit/controller/PhabricatorConduitTokenEditController.php

  • Committer: Package Import Robot
  • Author(s): Richard Sellam
  • Date: 2015-01-29 00:15:58 UTC
  • mfrom: (0.14.1) (0.13.1) (0.10.2) (2.1.4 sid)
  • Revision ID: package-import@ubuntu.com-20150129001558-7qklhtcc043y9mog
Tags: 0~git20150129-1
* New snapshot release
* restricted access to local config file (closes: #775479)
* moved local config file to /var/lib/phabricator (closes: #775478)
* switched mysql-server dependency to recommends (closes: #773536)
* use /run instead of /var/run (closes: #775803)
* prevent package reinstall from overwritting local changes (closes: #776288)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
final class PhabricatorConduitTokenEditController
 
4
  extends PhabricatorConduitController {
 
5
 
 
6
  public function handleRequest(AphrontRequest $request) {
 
7
    $viewer = $request->getViewer();
 
8
 
 
9
    $id = $request->getURIData('id');
 
10
    if ($id) {
 
11
      $token = id(new PhabricatorConduitTokenQuery())
 
12
        ->setViewer($viewer)
 
13
        ->withIDs(array($id))
 
14
        ->withExpired(false)
 
15
        ->requireCapabilities(
 
16
          array(
 
17
            PhabricatorPolicyCapability::CAN_VIEW,
 
18
            PhabricatorPolicyCapability::CAN_EDIT,
 
19
          ))
 
20
        ->executeOne();
 
21
      if (!$token) {
 
22
        return new Aphront404Response();
 
23
      }
 
24
 
 
25
      $object = $token->getObject();
 
26
 
 
27
      $is_new = false;
 
28
      $title = pht('View API Token');
 
29
    } else {
 
30
      $object = id(new PhabricatorObjectQuery())
 
31
        ->setViewer($viewer)
 
32
        ->withPHIDs(array($request->getStr('objectPHID')))
 
33
        ->requireCapabilities(
 
34
          array(
 
35
            PhabricatorPolicyCapability::CAN_VIEW,
 
36
            PhabricatorPolicyCapability::CAN_EDIT,
 
37
          ))
 
38
        ->executeOne();
 
39
      if (!$object) {
 
40
        return new Aphront404Response();
 
41
      }
 
42
 
 
43
      $token = PhabricatorConduitToken::initializeNewToken(
 
44
        $object->getPHID(),
 
45
        PhabricatorConduitToken::TYPE_STANDARD);
 
46
 
 
47
      $is_new = true;
 
48
      $title = pht('Generate API Token');
 
49
      $submit_button = pht('Generate Token');
 
50
    }
 
51
 
 
52
    if ($viewer->getPHID() == $object->getPHID()) {
 
53
      $panel_uri = '/settings/panel/apitokens/';
 
54
    } else {
 
55
      $panel_uri = '/settings/'.$object->getID().'/panel/apitokens/';
 
56
    }
 
57
 
 
58
    id(new PhabricatorAuthSessionEngine())->requireHighSecuritySession(
 
59
      $viewer,
 
60
      $request,
 
61
      $panel_uri);
 
62
 
 
63
    if ($request->isFormPost()) {
 
64
      $token->save();
 
65
 
 
66
      if ($is_new) {
 
67
        $token_uri = '/conduit/token/edit/'.$token->getID().'/';
 
68
      } else {
 
69
        $token_uri = $panel_uri;
 
70
      }
 
71
 
 
72
      return id(new AphrontRedirectResponse())->setURI($token_uri);
 
73
    }
 
74
 
 
75
    $dialog = $this->newDialog()
 
76
      ->setTitle($title)
 
77
      ->addHiddenInput('objectPHID', $object->getPHID());
 
78
 
 
79
    if ($is_new) {
 
80
      $dialog
 
81
        ->appendParagraph(pht('Generate a new API token?'))
 
82
        ->addSubmitButton($submit_button)
 
83
        ->addCancelButton($panel_uri);
 
84
    } else {
 
85
      $form = id(new AphrontFormView())
 
86
        ->setUser($viewer);
 
87
 
 
88
      if ($token->getTokenType() === PhabricatorConduitToken::TYPE_CLUSTER) {
 
89
        $dialog->appendChild(
 
90
          pht(
 
91
            'This token is automatically generated by Phabricator, and used '.
 
92
            'to make requests between nodes in a Phabricator cluster. You '.
 
93
            'can not use this token in external applications.'));
 
94
      } else {
 
95
        $form->appendChild(
 
96
          id(new AphrontFormTextControl())
 
97
            ->setLabel(pht('Token'))
 
98
            ->setValue($token->getToken()));
 
99
      }
 
100
 
 
101
      $dialog
 
102
        ->appendForm($form)
 
103
        ->addCancelButton($panel_uri, pht('Done'));
 
104
    }
 
105
 
 
106
    return $dialog;
 
107
  }
 
108
 
 
109
}