「cakephp」カテゴリーアーカイブ

メール送信

http://pc.casey.jp/archives/2087
http://d.hatena.ne.jp/tomoya/20100804/1280900579
http://blog.zolesystem.info/cakephp%E3%82%92%E3%81%A4%E3%81%8B%E3%81%A3%E3%81%A6%E3%83%A1%E3%83%BC%E3%83%AB%E3%82%92%E9%80%81%E3%82%8B/

http://d.hatena.ne.jp/nogusa/20110522/1306093773

http://log.noiretaya.com/151

http://blog.material-being.com/pages/entry/23

Config/email.php.defalt を email.phpに変更

Controllerに

[php]App::uses(‘CakeEmail’, ‘Network/Email’);
public $components = array(‘Email’);[/php]

送信アクションにController

[php]
public function send_mail(){
//Charset
$this->Email->charset = ‘utf-8’;

//toメールアドレス
$this->Email->to = ‘xxxxx.ss@gmail.com’;

//Fromメールアドレス
$this->Email->from = ‘xxxxxx.ss@gmail.com’;

//CC宛先(不要な場合はコメントアウト)
//$this->Email->cc = array(‘日本語CC宛先’.’xxxxxx@exmaple.com’);

//BCC宛先(不要な場合はコメントアウト)
//$this->Email->bcc = array(‘日本語BCC宛先’.’xxxxx@exmaple.com’);

//メールタイトル
$this->Email->subject = "【cakePHPテスト】メールタイトル";

//メール本文
$message =
"メールテスト:本文¥n"
."このメールはcakePHPを利用して送信しています。";

$this->Email->_lineLength = 40;

$this->Email->smtpOptions = array(
‘port’ => ‘465’, // 25, 587
‘timeout’ => ’30’,
‘host’ => ‘ssl://smtp.gmail.com’,
‘username’ => ‘xxxxxxx@gmail.com’,
‘password’ => ‘xxxxxx’
);
$this->Email->delivery = ‘smtp’;
//送信
$this->Email->send($message);
//exit;
}
[/php]

View/Pages/contact.ctp

[php]echo $this->Form->create(‘Contact’);
echo $this->Form->input(‘name’);
echo $this->Form->input(‘email’);
echo $this->Form->input(‘tel’);
echo $this->Form->input(‘body’);
echo $this->Form->end(‘送信’);?>
[/php]

View/Emails/text/contact.ctp

[php]名前:<?php echo $name; ?>
メール:<?php echo $email; ?>
電話番号:<?php echo $tel; ?>
内容
<?php echo $body; ?>[/php]

ACLを使わない簡単なアクセス制御

http://mojaie.hatenablog.jp/entry/2012/05/19/170024

bakeするときはgroupは後からカラム追加すると良いよ ※

[php]
CREATE TABLE IF NOT EXISTS `users` (
`id` int(10) NOT NULL,
`username` varchar(100) NOT NULL,
`group` varchar(100) NOT NULL,
`password` varchar(100) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`)
);
INSERT INTO `users` (`id`, `username`, `group`, `password`) VALUES(‘1’, ‘admin’, ‘admin’, ‘adminpassword’);
INSERT INTO `users` (`id`, `username`, `group`, `password`) VALUES(‘2’, ‘user’, ‘user’, ‘userpassword’);
[/php]

AppController

[php]
class AppController extends Controller {
public $helpers = array(
‘Html’,
‘Form’,
‘Session’,
);
public $components = array(
‘Session’,
‘Auth’ => array(

),
);
public function beforeFilter() {
$this->Auth->authorize = array(‘Controller’); //これを使うときはコントローラにbeforeFilterをからでも書くこと
$this->Auth->loginRedirect = ‘/’;
$this->Auth->logoutRedirect = ‘/’;
if ($this->Auth->loggedIn()) {
$this->set(‘authUser’, $this->Auth->user());
}
}
}
[/php]

UsersController

[php]
public function beforeFilter() {
}
/**
* login method
*/
public function login() {
$this->layout = ”;
/** POST送信時にログイン認証処理を行う */
if ($this->request->is(‘post’)) {
/** ログイン認証処理 */
if ($this->Auth->login()) {
/** 認証後のリダイレクト処理 */
$this->redirectlogin();
} else {
/** 認証エラーメッセージ出力 */
$this->Session->setFlash(__(‘ログインに失敗しました。’));
}
}else{
/** ログイン認証済の場合、顧客画面へ遷移 */
if ($this->Auth->login()) {
$this->redirectlogin();
}
}
}

/**
* redirectlogin method
*/
public function redirectlogin() {
try {
/** 顧客一覧ページのURLを格納 */
$redirectUrl = $this->Auth->redirect();
/** 顧客一覧ページにリダイレクト */
$this->redirect($redirectUrl);
} catch (Exception $e) {
/** エラー(ログアウト) */
$this->Auth->logout();
/** 認証エラーメッセージ出力 */
$this->Session->setFlash(__(‘ログインに失敗しました。’));
}
}

/**
* logout method
*/
public function logout() {
$this->Session->destroy();
$this->redirect($this->Auth->logout());
}
[/php]

権限をつけたいコントローラーに書くよ 

※これを書くと基本adminしかアクセスできなくなる

[php]
public function beforeFilter(){
//openaccessには誰でもアクセスできる
$this->Auth->allowedActions = array(‘openaccess’);
//親クラス(AppController)読み込み
parent::beforeFilter();
}

public function isAuthorized() {
if ($this->Auth->user(‘group’) == ‘admin’) {
//admin権限を持つユーザは全てのページにアクセスできる
return true;
} elseif ($this->Auth->user(‘group’) == ‘user’) {
if ($this->action == ‘useronly’) {
//user権限を持つユーザはuseronlyにアクセスできる
return true;
}
}
return false;
}

//誰でもアクセスできる
public function openaccess() {
}

//会員だけアクセスできる
public function useronly() {

}

//管理者だけアクセスできる
public function adminonly() {
}
[/php]

openaccess.ctpは誰でもアクセスできる

useronly.ctpは会員だけアクセスできる

adminonly.ctpは会員だけアクセスできる

Authで暗号化してたらログインできなかったので、暗号化解除したよ

http://aman.sakura.ne.jp/?p=782

ログインしているユーザー情報を取得

http://cakephp.jp/modules/newbb/viewtopic.php?topic_id=2573&forum=8

[php]
$userinfo = $this->Auth->user();
debug($userinfo);
[/php]

絞り込むぜいっ!

[php]
//ログインユーザーのIDとレクチャーのIDが同じやつ
$userinfo = $this->Auth->user();
debug($this->Lecture->find(‘all’,array(‘conditions’=>array(‘id’ => $userinfo))));
[/php]

または

[php]
$userinfo = $this->Auth->user(array(‘fields’ => ‘id’)); //ログインしているIDだけ取得
$this->set(‘children’, $this->Child->find(‘all’,array(‘conditions’=>array(‘user_id’=> $userinfo)))); //Childが持っているuser_idとログインのIDの同じやつ
[/php]

Sessionの使い方

core.phpに
[php]
//core.php
Configure::write(‘Session’, array(
‘defaults’ => ‘cake’ // tmpフォルダのsessionsフォルダに保存するよ
));
[/php]
コントローラーに
[php]
//function index
//Sessionにconditionを書き込んでおく
$this->Session->write(‘option’,$option);
[/php]
コントローラー
[php]
//検索条件をSessionから取得(indexで書き込んでる)
$option = $this->Session->read(‘option’);
[/php]