iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 13
0
Modern Web

PHP框架-Symfony4 + api platform 系列 第 13

Day#13 透過群組Group來進行讀或寫的權限區分

Group就是群組 ... 等等,這不是廢話嗎?


我們可以在api Resource的兩個地方加上Group名稱,接著將group賦予欄位

1.itemOperations method裡的 normalization_context 或是 denormalization_context

2.api Resource 的normalizationContext 或是denormalizationContext

第二個是設定集合的group,第一個因為是在itemOperations裡,所以當然是設定針對單一筆資料的group

通常加在itemOperations裡的group,會加個item,方便跟集合的group做區分


假設今天有個欄位被賦予的groups包含 test:write 跟 test:item:get ,
那這個欄位在集合POST可以寫入資料,因為集合沒有PUT和PATCH ,
在針對單一筆資料的GET時,可以讀取該欄位資料,因為是test:item:get 不是test:read

現在打開先前創建的實體類做個實驗吧~然後我們可以從api platform的介面上驗證結果 !

我們先給予這個實體類三個欄位,然後把建立好的group名稱賦予欄位

/**
 * @ORM\Entity
 * @ORM\Table(name="test_entity")
 * @ApiResource(
 *     collectionOperations={"get","post"},
 *     itemOperations={
 *          "get"={
 *              "normalization_context"={"groups"={"test:read", "test:item:get"}}
 *          },
 *          "put" = {
 *              "denormalization_context"={"groups"={"test:item:put"}}
 *          },
 *          "delete"
 *      }
 *     normalizationContext={"groups"={"test:read"},"enable_max_depth"="true","skip_null_values" = false},
 *     denormalizationContext={"groups"={"test:write"},"disable_type_enforcement"=true},
 * )
 * @TestCheck()
 */
class TestEntity
{

    /**
     * @var int
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     * @ORM\Column(name="id", type="integer")
     */
    private $id;


    /**
     * @ORM\Column(name="name",type="string",length=20,nullable=true)
     * @Assert\NotBlank()
     * @Assert\Length(max="20",min="5",maxMessage="長度不可超過20字",minMessage="長度不可低於5個字")
     * @Groups({"test:read" ,"test:write"})
     */
    private $name;


    /**
     * @ORM\Column(name="email",type="string",length=50,nullable=true)
     * @Assert\Email()
     * @Groups({"test:read"})
     */
    private $email;


    /**
     * @ORM\Column(name="phone",type="string",length=50,nullable=true)
     * @Groups({"test:read" ,"test:write","test:item:put"})
     */
    private $phone;

    /**
     * @return int
     */
    public function getId(): int
    {
        return $this->id;
    }

    /**
     * @param int $id
     */
    public function setId(int $id): void
    {
        $this->id = $id;
    }

    /**
     * @return mixed
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @param mixed $name
     */
    public function setName($name): void
    {
        $this->name = $name;
    }

    /**
     * @return mixed
     */
    public function getEmail()
    {
        return $this->email;
    }

    /**
     * @param mixed $email
     */
    public function setEmail($email): void
    {
        $this->email = $email;
    }

    /**
     * @return mixed
     */
    public function getPhone()
    {
        return $this->phone;
    }

    /**
     * @param mixed $phone
     */
    public function setPhone($phone): void
    {
        $this->phone = $phone;
    }


}

假設我賦予name和phone可以在post時寫入,api platform POST方法的try it out 點下去後就會像下圖


假設我賦予name,email,phone可以在集合的GET時讀取資料,集合GET方法的try it out點下去就會像下圖


假設我只賦予phone可以在針對單一筆資料PUT的時候寫入,PUT方法的try it out點下去就會像下圖

但因為我們將集合讀取資料的group套用在itemOperations,所以有test:read這個group的欄位,也可以在單一筆資料讀取的時候看見

這裡做個小提醒~ group可以加在property上,也可以加在get和set方法上 ,但小菜鳥我個人的習慣是加在property上

在使用Group的時候,別忘記import Symfony\Component\Serializer\Annotation\Groups; 哦~


相對前面幾篇,Group看起來是不是和藹多了!!

這篇提到關於欄位的Group設定,
下一篇的開頭來把validation 如何用group群組化驗證講完,
接著要來講Serialization 的機制,其實前面有稍微提到,但是光是稍微,沒辦法通靈阿....
所以還是做一下解釋好了: )


上一篇
Day#12 Api platform IRI 鎖鏈!? 咦,是酷拉皮卡的鎖鏈嗎?(๑´ڡ`๑)
下一篇
Day#14 資料型態轉來轉去是在轉什麼啦(/‵Д′)/~ ╧╧!!!? Serializer
系列文
PHP框架-Symfony4 + api platform 30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言