| 1 | <?php␊ |
| 2 | /* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */␊ |
| 3 | /*␊ |
| 4 | # ***** BEGIN LICENSE BLOCK *****␊ |
| 5 | # This file is part of InDefero, an open source project management application.␊ |
| 6 | # Copyright (C) 2008 CĂ©ondo Ltd and contributors.␊ |
| 7 | #␊ |
| 8 | # InDefero is free software; you can redistribute it and/or modify␊ |
| 9 | # it under the terms of the GNU General Public License as published by␊ |
| 10 | # the Free Software Foundation; either version 2 of the License, or␊ |
| 11 | # (at your option) any later version.␊ |
| 12 | #␊ |
| 13 | # InDefero is distributed in the hope that it will be useful,␊ |
| 14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of␊ |
| 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the␊ |
| 16 | # GNU General Public License for more details.␊ |
| 17 | #␊ |
| 18 | # You should have received a copy of the GNU General Public License␊ |
| 19 | # along with this program; if not, write to the Free Software␊ |
| 20 | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA␊ |
| 21 | #␊ |
| 22 | # ***** END LICENSE BLOCK ***** */␊ |
| 23 | ␊ |
| 24 | /**␊ |
| 25 | * Storage of the public keys (ssh or monotone).␊ |
| 26 | *␊ |
| 27 | */␊ |
| 28 | class IDF_Key extends Pluf_Model␊ |
| 29 | {␊ |
| 30 | public $_model = __CLASS__;␊ |
| 31 | ␊ |
| 32 | function init()␊ |
| 33 | {␊ |
| 34 | $this->_a['table'] = 'idf_keys';␊ |
| 35 | $this->_a['model'] = __CLASS__;␊ |
| 36 | $this->_a['cols'] = array(␊ |
| 37 | // It is mandatory to have an "id" column.␊ |
| 38 | 'id' =>␊ |
| 39 | array(␊ |
| 40 | 'type' => 'Pluf_DB_Field_Sequence',␊ |
| 41 | //It is automatically added.␊ |
| 42 | 'blank' => true,␊ |
| 43 | ),␊ |
| 44 | 'user' =>␊ |
| 45 | array(␊ |
| 46 | 'type' => 'Pluf_DB_Field_Foreignkey',␊ |
| 47 | 'model' => 'Pluf_User',␊ |
| 48 | 'blank' => false,␊ |
| 49 | 'verbose' => __('user'),␊ |
| 50 | ),␊ |
| 51 | 'content' =>␊ |
| 52 | array(␊ |
| 53 | 'type' => 'Pluf_DB_Field_Text',␊ |
| 54 | 'blank' => false,␊ |
| 55 | 'verbose' => __('public key'),␊ |
| 56 | ),␊ |
| 57 | );␊ |
| 58 | // WARNING: Not using getSqlTable on the Pluf_User object to␊ |
| 59 | // avoid recursion.␊ |
| 60 | $t_users = $this->_con->pfx.'users';␊ |
| 61 | $this->_a['views'] = array(␊ |
| 62 | 'join_user' =>␊ |
| 63 | array(␊ |
| 64 | 'join' => 'LEFT JOIN '.$t_users␊ |
| 65 | .' ON '.$t_users.'.id='.$this->_con->qn('user'),␊ |
| 66 | 'select' => $this->getSelect().', '␊ |
| 67 | .$t_users.'.login AS login',␊ |
| 68 | 'props' => array('login' => 'login'),␊ |
| 69 | )␊ |
| 70 | );␊ |
| 71 | }␊ |
| 72 | ␊ |
| 73 | function showCompact()␊ |
| 74 | {␊ |
| 75 | return Pluf_Template::markSafe(Pluf_esc(substr($this->content, 0, 25)).' [...] '.Pluf_esc(substr($this->content, -55)));␊ |
| 76 | }␊ |
| 77 | ␊ |
| 78 | private function parseContent()␊ |
| 79 | {␊ |
| 80 | if (preg_match('#^\[pubkey ([^\]]+)\]\s*(\S+)\s*\[end\]$#', $this->content, $m)) {␊ |
| 81 | return array('mtn', $m[1], $m[2]);␊ |
| 82 | }␊ |
| 83 | else if (preg_match('#^ssh\-[a-z]{3}\s(\S+)(?:\s(\S+))?$#', $this->content, $m)) {␊ |
| 84 | return array('ssh', $m[2], $m[1]);␊ |
| 85 | }␊ |
| 86 | ␊ |
| 87 | throw new Exception(__('Invalid or unknown key data detected.'));␊ |
| 88 | }␊ |
| 89 | ␊ |
| 90 | /**␊ |
| 91 | * Returns the type of the public key␊ |
| 92 | *␊ |
| 93 | * @return string 'ssh' or 'mtn'␊ |
| 94 | */␊ |
| 95 | function getType()␊ |
| 96 | {␊ |
| 97 | list($type, , ) = $this->parseContent();␊ |
| 98 | return $type;␊ |
| 99 | }␊ |
| 100 | ␊ |
| 101 | /**␊ |
| 102 | * Returns the key name of the key␊ |
| 103 | *␊ |
| 104 | * @return string␊ |
| 105 | */␊ |
| 106 | function getName()␊ |
| 107 | {␊ |
| 108 | list(, $keyName, ) = $this->parseContent();␊ |
| 109 | return $keyName;␊ |
| 110 | }␊ |
| 111 | ␊ |
| 112 | /**␊ |
| 113 | * This function should be used to calculate the key id from the␊ |
| 114 | * public key hash for authentication purposes. This avoids clashes␊ |
| 115 | * in case the key name is not unique across the project␊ |
| 116 | *␊ |
| 117 | * And yes, this is actually how monotone itself calculates the key␊ |
| 118 | * id...␊ |
| 119 | *␊ |
| 120 | * @return string␊ |
| 121 | */␊ |
| 122 | function getMtnId()␊ |
| 123 | {␊ |
| 124 | list($type, $keyName, $keyData) = $this->parseContent();␊ |
| 125 | if ($type != 'mtn')␊ |
| 126 | throw new Exception('key is not a monotone public key');␊ |
| 127 | return sha1($keyName.":".$keyData);␊ |
| 128 | }␊ |
| 129 | ␊ |
| 130 | function postSave($create=false)␊ |
| 131 | {␊ |
| 132 | /**␊ |
| 133 | * [signal]␊ |
| 134 | *␊ |
| 135 | * IDF_Key::postSave␊ |
| 136 | *␊ |
| 137 | * [sender]␊ |
| 138 | *␊ |
| 139 | * IDF_Key␊ |
| 140 | *␊ |
| 141 | * [description]␊ |
| 142 | *␊ |
| 143 | * This signal allows an application to perform special␊ |
| 144 | * operations after the saving of a public Key.␊ |
| 145 | *␊ |
| 146 | * [parameters]␊ |
| 147 | *␊ |
| 148 | * array('key' => $key,␊ |
| 149 | * 'created' => true/false)␊ |
| 150 | *␊ |
| 151 | */␊ |
| 152 | $params = array('key' => $this, 'created' => $create);␊ |
| 153 | Pluf_Signal::send('IDF_Key::postSave',␊ |
| 154 | 'IDF_Key', $params);␊ |
| 155 | }␊ |
| 156 | ␊ |
| 157 | function preDelete()␊ |
| 158 | {␊ |
| 159 | /**␊ |
| 160 | * [signal]␊ |
| 161 | *␊ |
| 162 | * IDF_Key::preDelete␊ |
| 163 | *␊ |
| 164 | * [sender]␊ |
| 165 | *␊ |
| 166 | * IDF_Key␊ |
| 167 | *␊ |
| 168 | * [description]␊ |
| 169 | *␊ |
| 170 | * This signal allows an application to perform special␊ |
| 171 | * operations before a key is deleted.␊ |
| 172 | *␊ |
| 173 | * [parameters]␊ |
| 174 | *␊ |
| 175 | * array('key' => $key)␊ |
| 176 | *␊ |
| 177 | */␊ |
| 178 | $params = array('key' => $this);␊ |
| 179 | Pluf_Signal::send('IDF_Key::preDelete',␊ |
| 180 | 'IDF_Key', $params);␊ |
| 181 | }␊ |
| 182 | }␊ |
| 183 | |