123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- <?php
- namespace App\Models;
- use Illuminate\Database\Eloquent\Model;
- /**
- * 该类调用时应该在http控制层限制管理员才能调用
- *
- * Class Role
- * @package App\Models
- */
- class Role extends Model
- {
- protected $table = "roles";
- public $timestamps = false;
- /**
- * 创建一个用户的角色
- *
- * @param array $params
- * @return array
- */
- public function CreateRole(array $params)
- {
- if ($params["description"] == null) {
- $params["description"] = "";
- }
- $this->name = $params["name"];
- $this->description = $params["description"];
- $this->status = "normal";
- if ($this->name == "") {
- return ["code" => EMPTY_ROLE_NAME];
- }
- // check if has the same role name in system.
- $role = $this->where("name", $this->name)->where("is_del", false)->first();
- if ($role) {
- return ["code" => ALREADY_EXIST_ROLE];
- }
- //
- $this->save();
- $params["id"] = $this->getQueueableId();
- return ["code" => SUCCESS, "data" => $params];
- }
- /**
- * 更新用户角色的名字,描述等信息
- *
- * @param array $params
- * @return int
- */
- public function ModifyRole(array $params)
- {
- $update = [];
- $id = $params["id"];
- if ($id == "") {
- return EMPTY_ROLE_ID;
- }
- // check if role exist
- $role = $this->where("id", $id)->where("is_del", false)->first();
- if (!$role) {
- return INVALID_ROLE_ID;
- }
- if ($params["name"] != "") {
- $update["name"] = $params["name"];
- }
- if ($params["description"] != "") {
- $update["description"] = $params["description"];
- }
- if (count($update) == 0) {
- return NOTHING_UPDATE;
- }
- $this->where("id", $id)->where("is_del", false)->update($update);
- return SUCCESS;
- }
- /**
- * 删除一个用户角色
- *
- * @param array $params
- * @return int
- */
- public function DeleteRole(array $params)
- {
- $id = $params["id"];
- if ($id == "") {
- return EMPTY_ROLE_ID;
- }
- $role = $this->where("id", $id)->where("is_del", false)->first();
- if (!$role) {
- return INVALID_ROLE_ID;
- }
- $this->where("id", $id)->where("is_del", false)->update(["is_del" => true]);
- return SUCCESS;
- }
- /**
- * 列出用户角色列表
- *
- * @param array $params
- * @return mixed
- */
- public function ListRole(array $params)
- {
- $page = $params["page"];
- $pageCount = $params["pageCount"];
- if ($page < 1) {
- $page = 1;
- }
- if ($pageCount > 15 || $pageCount < 1) {
- $pageCount = 15;
- }
- $data = $this->select("name", "description")
- ->where("is_del", false)->orderBy("created_at", "asc")
- ->paginate($pageCount, ["*"], "page", $page);
- return $data;
- }
- }
|