Jump to content
nahanil

Accessing encryption/decryption methods in a module

Recommended Posts

Hey all,

Just wondering if there's a Right Way™ to access the systemEncrypt/systemDecrypt methods from within a module (specifically, a forked version of the Plesk service module).

I've hacked it in for now, but it feels dirty and though it works I was wondering if there's a better way.

Current 'solution':

  /**
   * Wrapper around systemEncrypt function
   *
   * @param string $string The string to encrypt
   * @return string The encrypted version of input string
   */
  protected function encryptString($string) {
    if (!isset($this->Clients))
      Loader::loadModels($this, array("Clients"));
    return $this->Clients->systemEncrypt($string);
  }

  /**
   * Wrapper around systemDecrypt function
   *
   * @param string $string The string to decrypt
   * @return string The decrypted version of input string
   */
  protected function decryptString($string) {
     if (!isset($this->Clients))
      Loader::loadModels($this, array("Clients"));
    return $this->Clients->systemDecrypt($string);
  }

I don't actually use the Clients model anywhere, just load it so that I can have access to the systemEncrypt/systemDecrypt methods that are attached.

I suppose the real question would be is it possible to add a bonafide custom Model to a module, as I'm storing the encrypted values in a table the module is creating for itself during install/upgrade.

ie

  public function upgrade($current_version) {
    if (version_compare($this->getVersion(), $current_version, ">")) {
      // Upgrade to v1.0.2
      if (version_compare($current_version, "0.2.3", "<")) {
        $this->Record->
          setField("id", array('type' => "int",'size' => 10,'unsigned' => true,'auto_increment' => true))->
          setField("client_id", array('type' => "int", 'size' => 10))->
          setField("encrypted_field", array('type' => "varchar", 'size' => 255))->
...
          setKey(array("id"), "primary")->
...
          create("my_custom_table");
      }
    }
  }
    $some_data = $this->Record->select()->from("my_custom_table")
      ->where("client_id", "=", $service->client_id)
      ->fetch();
    $some_data->encrypted_field = $this->decryptString($some_data->encrypted_field);

Not sure how I could wrap this up and have $this->MyModel->foo(), but I assume if it's possible it'd extend some base model class which would give me the encryption methods 'For Free'.

Edited by texh
Added more of my example code to hopefully clarify what I'm doing/what I'd like to do

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...