03 January 2012

Setup Gitolite Hook post-receive-email

設定 Gitolite 範圍中每個 repository 在每次收到 git push 之後就通知該 repository 相關人員,需要設定 hook 機制。但是 gitolite 和 git 的設定方式有些不同,尋覓許多文件後終於找到實作的方式成功 :)


環境
  • 建立 gitolite 環境:參考 Tsung 的「Linux 使用 Gitolite 架設 Git Server」 採用 gitolite  這個帳號直接管理 gitolite,沒有另外建立一個 git 的 linux user 帳號。
  • gitolite 的 hooks 位在:/usr/share/gitolite/hooks/common
  • 寄信通知大家的工具程式「post-receive-email」位在:/usr/share/doc/git/contrib/hooks/post-receive-email

方向
  • 讓 gitolite 的 hook script 裡頭知道要去哪裡找到可以執行送信程式「post-receive-email」
$ sudo chmod a+x /usr/share/doc/git/contrib/hooks/post-receive-email
$ sudo vim /usr/share/gitolite/hooks/common/post-receive
$ sudo chmod a+x /usr/share/gitolite/hooks/common/post-receive
在 /usr/share/gitolite/hooks/common/post-receive 中,原本是空的,可以輸入以下 script:
#!/bin/bash
/usr/share/doc/git/contrib/hooks/post-receive-email

  • 調整 .gitolite.rc 檔案內容(不然等一下設定 git config 參數在 gitolite.conf 的時候,gitolite 會不認識 config 參數)
$ sudo su gitolite
$ vim .gitolite.rc
  # 然後找到「$GL_GITCONFIG_KEYS = "";」
  # 改成「$GL_GITCONFIG_KEYS = ".*";」
$ gl-setup admin.pub
  • 將 git config 參數設定在 gitolite-admin/conf/gitolite.conf 裡頭,然後 git push 
repo    testing # 在此以 testing 這個 repo 為範例
        RW+     = @all # 你原本的權限設定

        config hooks.mailinglist    = "ernest@example.com, chiang@example.com" # 可以寄信給多個人
        config hooks.envelopesender = git@example.com # 寄件者的信箱地址 email address of sender
        config hooks.emailprefix    = "[testing] " # 信件主旨的開頭字串
        config hooks.showrev        = "git show -C %s; echo" # 原本只有列出有改動的檔案們,在此我改為列出有改動的檔案內容
然後 git add, git commit, git push 到 git server 去。
  • git push 後,在 git server 端,用 gitolite 這個 user 跑 gl-setup (讓 gitolite 幫我們將 config 和 hooks 灑進去各個 repository 中)
$ sudo su gitolite
$ gl-setup admin.pub

到目前為止應該 hook 可以正常動作了 :)

參考參數:
(reference)


# Config
# ------
# hooks.mailinglist
#   This is the list that all pushes will go to; leave it blank to not send
#   emails for every ref update.
# hooks.announcelist
#   This is the list that all pushes of annotated tags will go to.  Leave it
#   blank to default to the mailinglist field.  The announce emails lists
#   the short log summary of the changes since the last annotated tag.
# hooks.envelopesender
#   If set then the -f option is passed to sendmail to allow the envelope
#   sender address to be set
# hooks.emailprefix
#   All emails have their subjects prefixed with this prefix, or "[SCM]"
#   if emailprefix is unset, to aid filtering
# hooks.showrev
#   The shell command used to format each revision in the email, with
#   "%s" replaced with the commit id.  Defaults to "git rev-list -1
#   --pretty %s", displaying the commit id, author, date and log
#   message.  To list full patches separated by a blank line, you
#   could set this to "git show -C %s; echo".
#   To list a gitweb/cgit URL *and* a full patch for each change set, use this:
#     "t=%s; printf 'http://.../?id=%%s' \$t; echo;echo; git show -C \$t; echo"
#   Be careful if "..." contains things that will be expanded by shell "eval"
#   or printf.
# hooks.emailmaxlines
#   The maximum number of lines that should be included in the generated
#   email body. If not specified, there is no limit.
#   Lines beyond the limit are suppressed and counted, and a final
#   line is added indicating the number of suppressed lines.
#


參考文件:





29 September 2011

Drupal 7: 使用者更改密碼後,如何取得新密碼?

今天在整合某一個 Drupal 7 與另一個系統之間帳號同步的模組,由 Drupal 7 作為前端使用者介面,當使用者變更密碼的時候,將使用者新的密碼同步到另一個系統去。(另一個系統相當老舊,沒辦法走其他比較安全的 Auth 認證方式。)本來想要用 form alter callback 來處理的(但真的還不熟悉 drupal form 的處理順序,一直拿到舊的資料),後來發現也可以直接透過 field module (hook_field_attach_submit) 來協助即可,原始 hint 出處 <Get new password in cleartext when a user changes it in Drupal?>。

Stackoverflow 是程式設計師的好幫手 :)

以下範例是放置在一個 Drupal custom module 裡頭:


/**
 * Implement hook_field_attach_submit().
 */
function yourModuleName_field_attach_submit($entity_type, $entity, $form, &$form_state) {
  if ($entity_type == 'user') {
    // handle your $form["#user"]->pass;
    debug($form["#user"]->pass);
  }
}

Brought to you by

26 September 2011

Drupal 7: 如何修改調整使用者註冊表單?取消帳號名稱,改用 Email 欄位?

user_register_form 是個很有趣,也很方便當做 Drupal Module 開發入門的切入點。大部分客戶需求的修改,都可以透過變更 $form 這個有點複雜的陣列 (array) 內容達成目的。今天在這舉個簡單的例子。

客戶需求:

  • 希望採用 email 作為使用者帳號,方便未來串接 OpenID 等會員認證方式。
  • 因為採用 email 作為使用者帳號,希望取消原本的「會員帳號名稱」欄位。


系統背景狀況:

  • 在一個 Drupal 7 環境中,已安裝正體中文語系與翻譯。
  • 在一個 Drupal 7 環境中,已安裝模組「LoginToboggan」,並設定:
    • 「Allow users to login using their e-mail address」:Enabled
    • 有客製化翻譯部分 LoginToboggan 的前台用字語句。
練習一:觀察會員註冊表單 $form 的內容
  • 檔案位置:該 Drupal 7 系統中所使用的 theme 目錄底下的 template.php
function [your_theme_name]_form_user_register_form_alter(&$form, &$form_state, $form_id) {

  print_r($form);

}
然後到 Drupal 7 網站前端觀察這一頁「http://your_website_domain/user/register」,應該可以看到完整的 $form 內容,以下擷取一小段我的環境中的片段範例:
Array ( 
  [#user] => stdClass Object ( 
    [uid] => 0 
    [hostname] => xxx.xxx.xxx.xxx
    [roles] => Array ( [1] => anonymous user ) 
    [cache] => 0 ) 
  [#user_category] => register 
  [#attached] => Array ( 
    [library] => Array ( 
      [0] => Array ( 
        [0] => system 
        [1] => jquery.cookie ) ) ) 
  [#attributes] => Array ( 
    [class] => Array ( 
      [0] => user-info-from-cookie ) ) 
  [#validate] => Array ( 
    [0] => user_account_form_validate 
    [1] => user_validate_picture 
    [2] => user_register_validate 
    [3] => logintoboggan_user_register_validate ) 
  [account] => Array ( 
    [#weight] => -10 
    [name] => Array ( 
      [#type] => textfield 
      [#title] => 使用者名稱 
      [#maxlength] => 60 
      [#description] => 可以使用空格; 除了半形句點、連結線和底線 (. - _) 之外,不可使用其他符號。 
      [#required] => 1 
      [#attributes] => Array ( 
        [class] => Array ( 
          [0] => username ) ) 
      [#default_value] => 
      [#access] => 1 
      [#weight] => -30 ) 
    [mail] => Array ( 
      [#type] => textfield 
      [#title] => 電子郵件地址 
      [#maxlength] => 254 
      [#description] => 一個有效的電子郵件地址,所有由系統發出的電子郵件將往此地址。此電子郵件地址位址不會被公開,並且只在您想要申請新的密碼,或是接收某些新聞或通知時,我們才會用此電子郵件地址寄信給您。 
      [#required] => 1 
      [#default_value] => 
      [#weight] => -29 ) 
    [pass] => Array ( 
      [#type] => password_confirm 
      [#size] => 25 
      [#description] => 為新帳號設定密碼。請輸入兩次密碼。 密碼長度至少需有 6 個字元。 
      [#required] => 1 ) 

這當中,我們可以觀察到「account」 有兩個屬性「required」和「access」是可以動手腳來達成我們所需要的目的。嘗試看看在 theme 目錄底下的 template.php 中做些調整:
function [your_theme_name]_form_user_register_form_alter(&$form, &$form_state, $form_id) {

  $form['account']['name']['#access'] = FALSE;
  $form['account']['name']['#required'] = FALSE;

}

即可達成這次的客製化任務 :)
9/26: 看來還需要處理 form submit 之後。目前暫時解法是把 email 中的 username 字段直接送到 account name 欄位去,以避開 form validate。再找看看有沒有比較漂亮的解法。

Brought to you by

23 September 2011

搶先試玩 Facebook Timeline

Facebook Timeline

Facebok 在 f8 發表 Facebook Timeline 新介面之後,很多人躍躍欲試。剛才看了 TechOrange 的介紹之後很輕鬆地完成了介面更新 :)

因為之前工作的關係,已經有註冊過 Facebook Developer,所以只要到 Facebook Developer 填寫 Open Graph 段落,每個步驟都按「Save Changes and Next」,最後按一下「Save and Finish」 即可,返回原本 Facebook 個人 Profile page 即可看到更新到 Facebook Timeline 的提示畫面囉!

新的 Facebook Timeline 介面還滿喜歡的,但是分成左右兩欄不是直覺的時間彙整(之前讀過的 wall item 是在左邊,但過些時候再去閱讀時可能會在右邊或左邊,這對於我這種有位置記憶的人來說是種困擾),閱讀起來有點辛苦。但是整體整合的概念很喜歡。

新的介面完成之後會有個簡短的說明:
Your timeline is now live — Developer Release.Please note: During the developer release, only other developers will be able to see your new timeline. Everyone else will see your old profile.
看來最近 Facebook Developer 人數會增加一些,哈 :p


Drupal 7 錯誤訊息處理: Add new field: you need to select a widget.

之前為了要在 Drupal 7 當中增加使用者的相關屬性欄位(user profile field),例如使用者的英文名字、個人網站之類的個人資訊,很直覺的到 Home » Administration » Configuration » People » Account settings » Manage fields 這個位置新增一個欄位給使用者資訊(user profile)使用,但在新增欄位時(Add new field)卻會出現錯誤訊息「Add new field: you need to select a widget.」。嗯好,遇到了狀況(bug),就來處理看看囉(debug)。

當時的背景資訊是:

  • 有新增並啟用客製化的模版(theme)
  • 有新增並啟用客製化的模組(module)
  • 沒有動到 Drupal Core


幾個直覺反應是:
  • 可能是 Drupal 7 的 bug?
  • 有發現在新增欄位時(Add new field)最右側的下拉式選單(select list, options)是沒有啟用的狀態,無法點選。但是文件資料上看到的畫面都是可以下拉做選取的。推斷可能跟 JavaScript 有關連。

解有錯誤訊息的狀況時,通常可以先從:直接將錯誤訊息丟進搜尋引擎找看看有沒有前人處理過的經驗。很幸運的在看了兩三篇文件後就找到了方向,是可能因為 jQuery 方面採用了新版本的 jQuery 所造成的狀況(所以在我套用自製模版與模組之後,才發生這個狀況),但後來有看到一個比較乾脆的解法:修改檔案 modules/field_ui/field_ui.js 大約 89 行附近的這一行,即可解決:

$(this).html(html).attr('disabled', disabled ? 'disabled' : '');
改成:
$(this).html(html).attr('disabled', disabled ? 'disabled' : false);


Brought to you by

LinkWithin

Related Posts with Thumbnails