我正在构建一个连接到我的远程服务器的 Android 应用程序。我允许用户添加新内容,但在允许添加之前还必须对其进行身份验证。这是他们添加的示例:
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair(SharedVariables.TAG_NAME, name));
params.add(new BasicNameValuePair(SharedVariables.TAG_ADDRESS, address));
params.add(new BasicNameValuePair(SharedVariables.TAG_TYPE, type));
params.add(new BasicNameValuePair(SharedVariables.TAG_DESCRIPTION, description));
params.add(new BasicNameValuePair(SharedVariables.TAG_DIFFICULTY, difficulty));
params.add(new BasicNameValuePair(SharedVariables.TAG_TERRAIN, terrain));
params.add(new BasicNameValuePair(SharedVariables.TAG_LONG, Double.toString(currentLng)));
params.add(new BasicNameValuePair(SharedVariables.TAG_LAT, Double.toString(currentLat)));
params.add(new BasicNameValuePair(SharedVariables.TAG_UID, Integer.toString(2)));
//Retrieve hash and id to identify authenticate user
UserAuth auth = new UserAuth(AddSpot.this);
HashMap<String,String> session = auth.getUserDetails();
params.add(new BasicNameValuePair(SharedVariables.TAG_UID, session.get("id")));
params.add(new BasicNameValuePair(SharedVariables.TAG_HASH, session.get("hash")));
如您所见,在第一部分中,我存储了用户自己添加的内容,但是,然后我还将用户 ID 和令牌/哈希传递到 POST 数据中,如上一部分所示。我的问题是,我不想将所有内容添加到数据库表中$this->Location->save($this->request->data)
。相反,我希望能够挑选出用户 ID 和哈希并将其删除,然后将其保存到所有其他内容中。我怎样才能在 CakePHP 中做到这一点?
这是我现在在 CakePHP 中的方法:
function add()
{
if ($this->request->is('post'))
{
$response = array();
$this->loadModel('User');
if($this->User->validateHash($id, $hash))
{
//Do some logic to remove id and hash from request
$this->Location->create();
//Save data from POST
if ($this->Location->save($this->request->data))
{
//Successfully saved
$this->response->statusCode(200);
}
else
{
//Error in saving
$this->response->statusCode(500);
}
}
else
{
//Does their hash match? No, then they are unauthorized to add
$this->response->statusCode(401);
}
}
$this->set(compact('response'));
$this->set('_serialize', array('response'));
}