/** * 控制器执行主逻辑函数 * * @return mixed $value 返回最终需要执行完的结果 */ public function invoke($uri = null) { $user = MUserManager::getInstance()->getCurrentUser(); $device = MUserManager::getInstance()->getCurrentDevice(); $user_id = $user["user_id"]; $user_name = $user["user_name"]; $device_uuid = $device["user_device_uuid"]; $device_name = $device["user_device_name"]; $db_user_device = MiniUserDevice::getInstance()->getByUuid($device_uuid); if ($db_user_device === NULL) { throw new MFileopsException(Yii::t('api', 'the device does not match'), MConst::HTTP_CODE_403); } $id = $db_user_device["id"]; $ret = MiniUserDevice::getInstance()->deleteDevice($db_user_device["id"]); if ($ret === false) { throw new MFileopsException(Yii::t('api', 'remove the device failure'), MConst::HTTP_CODE_403); } $response = array(); $response["stataus"] = "ok"; $response["did"] = $device_uuid; $response["display_name"] = $device_name; $response["uid"] = $user_id; $response["user_name"] = $user_name; echo json_encode($response); }
/** * 根据id获得User完整信息,在整个会话期内只有一个User对象 * @param $token */ public function getUserDevice2($id) { if ($this->hasCache2 === true && $this->device !== NULL) { Yii::trace("cache 2 get deviceId:" . $id, "miniyun.cache2"); return $this->device; } $this->device = MiniUserDevice::getInstance()->getUserDevice($id); return $this->device; }
/** * 获取events数据 */ public function getList($path, $time, $deviceUuid, $pageSize, $currentPage) { $user = $this->user; $userId = $user['id']; $time = $this->getTime($time); if ($path != "") { $path = MiniUtil::joinPath($path); } $total = MiniEvent::getInstance()->getTotal($path, $time, $userId, $deviceUuid); $totalPage = ceil($total / $pageSize); $events = MiniEvent::getInstance()->getByCondition($path, $userId, $time, $deviceUuid, $pageSize, ($currentPage - 1) * $pageSize); $itemList = array(); $data = array(); foreach ($events as $event) { $item = array(); $device = MiniUserDevice::getInstance()->getUserDevice($event['user_device_id']); $item['create_user_id'] = $device['user_id']; $item['file_path'] = MiniUtil::getRelativePath($event['file_path']); $item['action'] = $event['action']; $item['user_name'] = $user['user_name']; $item['user_device_type'] = $device['user_device_type']; if ($device['user_id'] == $userId) { $item['user_self'] = true; } else { $item['user_self'] = false; $user = MiniUser::getInstance()->getById($device['user_id']); $userMetas = MiniUserMeta::getInstance()->getUserMetas($device['user_id']); if (isset($userMetas['nick'])) { $item['user_name'] = $userMetas['nick']; } else { $item['user_name'] = $user['user_name']; } } $item['created_at'] = MiniUtil::formatTime(strtotime($event['created_at'])); $item['user_device_name'] = $device['user_device_name']; $item['context'] = MiniUtil::getRelativePath($event['context']); $item['device_uuid'] = $device['user_device_uuid']; if ($event['action'] == 2) { //判断是否是重命名还是创建 $fromParent = CUtils::pathinfo_utf($event['file_path']); $toParent = CUtils::pathinfo_utf($event['context']); if ($fromParent['dirname'] == $toParent['dirname']) { $item['action'] = MConst::RENAME; } } $itemList[] = $item; } $data['events'] = $itemList; $data['totalPage'] = $totalPage; return $data; }
/** * 存储前设置ID,因为这里要重用被删除的ID * @see CActiveRecord::beforeSave() */ public function beforeSave() { if (parent::beforeSave()) { if ($this->isNewRecord) { //如果是添加记录,而且Options表存在上次被删除的用户ID,则重复使用 $currentId = MiniUserDevice::getInstance()->getTemporaryId(); if (!empty($currentId)) { $this->id = $currentId; } } return true; } return false; }
public function getOnlineUsers($refresh = false) { $data = array(); if (empty($this->items) || $refresh) { $devices = MiniOnlineDevice::getInstance()->getOnlineDevices(); foreach ($devices as $item) { $appId = $item["application_id"]; $device = MiniUserDevice::getInstance()->getUserDevice($item["device_id"]); $user = MiniUser::getInstance()->getUser($device["user_id"]); $online = array("name" => $user["user_name"], "nick" => $user['nick'], "appname" => $appId, "deviceName" => $device["user_device_name"], "deviceType" => $device["user_device_type"], "lastLoginTime" => $item["updated_at"], "avatar" => $user["avatar"]); array_push($this->items, $online); } } $data['list'] = $this->items; $data['total'] = MiniOnlineDevice::getInstance()->getOnlineCount(); return $data; }
/** * * 在用户验证失败后是否需要进行自身用户系统的验证 */ public static function getDevice($userId, $deviceType, $deviceName, $deviceInfo) { //生成设备的uuid if ($deviceType == MConst::DEVICE_WEB) { $deviceUuid = MiniUtil::getDeviceUUID("web", $deviceType, "web", $userId); } else { $deviceUuid = MiniUtil::getDeviceUUID($deviceInfo, $deviceType, $deviceName, $userId); } //存在用户指定设备则通过 $device = NULL; if ($deviceType == MConst::DEVICE_WEB) { $device = MiniUserDevice::getInstance()->getWebDevice($userId); } else { $device = MiniUserDevice::getInstance()->getByUuid($deviceUuid); } if (isset($device)) { return $device; } //生成设备 $device = MiniUserDevice::getInstance()->create($userId, $deviceUuid, $deviceType, $deviceInfo, $deviceName); return $device; }
/** * 历史版本恢复 * @param int $deviceId * @param string $filePath * @param string $signature * @return bool */ public function recover($deviceId, $filePath, $signature) { $version = MiniVersion::getInstance()->getBySignature($signature); $file = $this->getModelByPath($filePath); if ($version["id"] == $file['version_id']) { return true; } $device = MiniUserDevice::getInstance()->getById($deviceId); $userId = $device["user_id"]; $user = MiniUser::getInstance()->getUser($userId); $userNick = $user["nick"]; // events表 相关操作 $userDeviceName = $device["user_device_name"]; $userDeviceId = $device["id"]; $signature = $version['file_signature']; $action = CConst::MODIFY_FILE; $file->file_update_time = time(); $context = array('hash' => $signature, 'rev' => (int) $version["id"], 'bytes' => (int) $version['file_size'], 'update_time' => (int) $file->file_update_time, 'create_time' => (int) $file['file_create_time']); $filePath = $file['file_path']; $eventUuid = MiniUtil::getEventRandomString(CConst::LEN_EVENT_UUID); MiniEvent::getInstance()->createEvent($file['user_id'], $userDeviceId, $action, $filePath, serialize($context), $eventUuid); //create event // files表相关操作 $file->version_id = $version["id"]; $file->event_uuid = $eventUuid; $file->file_size = $version['file_size']; $file->save(); // meta表相关操作 $fileMeta = FileMeta::model()->find('file_path = ?', array($filePath)); $versions = CUtils::getFileVersions($userDeviceName, $version['file_size'], $version["id"], CConst::WEB_RESTORE, $userId, $userNick, $fileMeta['meta_value']); $fileMeta->meta_value = $versions; $fileMeta->save(); //更新版本引用数 MiniVersion::getInstance()->updateRefCountByIds(array($version["id"]), TRUE); return true; }
/** * 添加登陆日志 * @param $deviceId * @return Logs */ public function createLogin($deviceId) { if (empty($deviceId)) { return; } $device = MiniUserDevice::getInstance()->getById($deviceId); $logs = new Logs(); $logs['message'] = $this->getIP(); //当前登陆用户的IP $logs['user_id'] = $device["user_id"]; $logs['type'] = 0; $arr = array("action" => MConst::LOGIN, "device_id" => $deviceId, "device_type" => $device["user_device_type"]); $logs['context'] = serialize($arr); $logs->save(); MiniUserDevice::getInstance()->updateLastModifyTime($deviceId); return $logs; }
/** * 获得当前登录的设备 * @param $user * @return null */ private function getCurrentDevice($user) { $deviceType = 1; if (MiniHttp::isPCClient()) { if (MiniHttp::isWindowsOS()) { $deviceType = 2; //Windows 客户端 } else { if (MiniHttp::isMacOS()) { $deviceType = 3; //Mac 客户端 } else { $deviceType = 5; //Linux 客户端 } } } //对设备进行检测 if ($deviceType == 1) { $device = DeviceManager::getDevice($user["id"], MConst::DEVICE_WEB, "web", $_SERVER['HTTP_USER_AGENT']); } else { $device = MiniUserDevice::getInstance()->getFirstByDeviceTypeAndDeviceName($user["id"], $deviceType); } return $device; }
/** * 把用户删除 * @param int $userId */ public function deleteUser($userId) { $user = User::model()->findByPk($userId); if (isset($user)) { //管理员账户不能被删除 if ($user->user_name == 'admin' || $userId == 1) { return; } //清理Cache $this->cleanCache($userId); //删除用户元数据信息 MiniUserMeta::getInstance()->deleteMeta($userId); //删除用户权限数据 MiniUserPrivilege::getInstance()->deletePrivilege($userId); //删除用户设备信息 MiniUserDevice::getInstance()->deleteDeviceByUser($userId); //把用户所有的事件信息删除 MiniEvent::getInstance()->deleteByUserId($userId); //把userId资源暂存到Options表中 $this->temporary2Option($userId); //删除自己,这里不能修改为sql模式,因为用户ID在删除的时候,自动将ID记录到了Options表中 $user->delete(); } }
/** * 根据UUID删除设备 * @param $deviceUUid * @return bool */ public function deleteDevice($deviceUUid) { $device = $this->device; if ($device["user_device_uuid"] == $deviceUUid) { return false; } MiniUserDevice::getInstance()->deleteDeviceByUuid($deviceUUid); return true; }
/** * 根据openId获得accessToken */ public function bindOpenId($appKey, $openId) { $user = MiniUser::getInstance()->getUserByOpenId($openId); if (!empty($user)) { $webDevice = MiniUserDevice::getInstance()->getWebDevice($user["id"]); if (!empty($webDevice)) { $token = MiniToken::getInstance()->getToken4Login($appKey, $webDevice["id"]); if (!empty($token)) { return array("success" => true, "access_token" => $token["oauth_token"]); } } } return array("success" => false); }
/** *根据用户名或设备名查找设备信息 */ public function searchDevicesByName($key, $pageSize, $currentPage) { $ids = ''; $sql_str = "select mud.id from " . DB_PREFIX . "_user_devices mud"; $sql_str .= " where mud.user_device_name like \"%{$key}%\""; $sql = Yii::app()->db->createCommand($sql_str); $byDeviceName = $sql->queryAll(); $sql_str = " select mud.id from " . DB_PREFIX . "_user_devices mud," . DB_PREFIX . "_users mu"; $sql_str .= " where mud.user_id=mu.id and mu.user_name like \"%{$key}%\""; $sql = Yii::app()->db->createCommand($sql_str); $byUserName = $sql->queryAll(); $sql_str = " select mud.id from " . DB_PREFIX . "_user_devices mud," . DB_PREFIX . "_user_metas mum"; $sql_str .= " where mud.user_id=mum.user_id and mum.meta_key='nick' and mum.meta_value like \"%{$key}%\""; $sql = Yii::app()->db->createCommand($sql_str); $byNick = $sql->queryAll(); $devices = array_merge($byDeviceName, $byUserName, $byNick); foreach ($devices as $device) { if (strpos($ids, $device['id']) === false) { $ids .= $device['id']; $ids .= ','; } } $len = strlen($ids); if ($len <= 0) { return "0"; } $ids = substr($ids, 0, $len - 1); $idsArr = explode(',', $ids); //分页获取数据 if ($pageSize < count($idsArr)) { $idsArr = array_slice($idsArr, ($currentPage - 1) * $pageSize, $pageSize); } $data = array(); $list = array(); foreach ($idsArr as $deviceId) { $device = MiniUserDevice::getInstance()->getById($deviceId); $dev = array(); $user = MiniUser::getInstance()->getUser($device['user_id']); $dev['is_admin'] = $user['is_admin']; $dev['device_id'] = $device['id']; $dev['user_id'] = $device['user_id']; $dev['userName'] = $user['nick']; $dev['avatar'] = $user['avatar']; $dev['userDeviceType'] = $device['user_device_type']; $dev['userDeviceName'] = $device['user_device_name']; $dev['lastLoginTime'] = $device['updated_at']; array_push($data, $dev); } $list['list'] = $data; $list['total'] = count($idsArr); return $list; }
/** * 根据created_at来获取事件 */ public function getByCondition($filePath, $userId, $time, $deviceUuid, $limit, $offset) { $criteria = new CDbCriteria(); $criteria->condition = 'user_id = :userId'; $criteria->params = array('userId' => $userId); if ($time !== "-1") { $criteria->addCondition("created_at <=:created_at", "and"); $criteria->params[':created_at'] = $time; } if ($deviceUuid !== "-1") { $device = MiniUserDevice::getInstance()->getByDeviceUuid($deviceUuid); $criteria->addCondition("user_device_id =:user_device_id", "and"); $criteria->params[':user_device_id'] = $device["id"]; } if ($filePath !== "") { $criteria->addCondition("file_path like '" . $filePath . "/%'", "and"); } $criteria->order = '-id'; $criteria->limit = $limit; $criteria->offset = $offset; $items = Event::model()->findAll($criteria); return $this->db2list($items); }