Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
userfield.php
1<?php
2namespace Bitrix\Rest\Api;
3
6
8{
9 private static $nameFullPrefix = 'UF_USR_';
10 private const ENTITY_ID = 'USER';
11 private const ALLOWED_FIELD_PROP_LIST = [
12 'FIELD_NAME',
13 'USER_TYPE_ID',
14 'XML_ID',
15 'MULTIPLE',
16 'SHOW_FILTER',
17 'SORT',
18 'LABEL',
19 'LIST_FILTER_LABEL',
20 'LIST_COLUMN_LABEL',
21 'EDIT_FORM_LABEL',
22 'ERROR_MESSAGE',
23 'HELP_MESSAGE',
24 'SETTINGS',
25 'LIST',
26 ];
27 public const SCOPE_USER_USERFIELD = 'user.userfield';
28 protected $namePrefix = 'USR';
29
30 public static function getTargetEntityId()
31 {
32 return static::ENTITY_ID;
33 }
34
35 public static function addRest($query, $n, \CRestServer $server)
36 {
37 $fields = [];
38
39 $query = array_change_key_case($query, CASE_UPPER);
40 if (is_array($query['FIELDS']))
41 {
42 $fields = static::checkFields($query['FIELDS']);
43 }
44
45 $instance = new static(static::getTargetEntityId());
46
47 return $instance->add($fields);
48 }
49
50 public static function updateRest($query, $n, \CRestServer $server)
51 {
52 $query = array_change_key_case($query, CASE_UPPER);
53 $id = (int) $query['ID'];
54 if ($id <= 0)
55 {
56 throw new RestException('ID is not defined or invalid.');
57 }
58
59 if (!static::checkAccessField($id))
60 {
61 throw new RestException('Access denied.');
62 }
63
64 $fields = [];
65 if (is_array($query['FIELDS']))
66 {
67 $fields = static::checkFields($query['FIELDS']);
68 }
69
70 $instance = new static(static::getTargetEntityId());
71
72 return $instance->update($id, $fields);
73 }
74
75 public static function deleteRest($query, $n, \CRestServer $server)
76 {
77 $query = array_change_key_case($query, CASE_UPPER);
78 $id = (int) $query['ID'];
79 if ($id <= 0)
80 {
81 throw new RestException('ID is not defined or invalid.');
82 }
83
84 if (!static::checkAccessField($id))
85 {
86 throw new RestException('Access denied.');
87 }
88
89 $instance = new static(static::getTargetEntityId());
90
91 return $instance->delete($id);
92 }
93
94 public static function getListRest($query, $n, \CRestServer $server)
95 {
96 $order = [];
97 $filter = [];
98 $query = array_change_key_case($query, CASE_UPPER);
99 if (is_array($query['ORDER']))
100 {
101 $order = $query['ORDER'];
102 }
103 if (is_array($query['FILTER']))
104 {
105 $filter = $query['FILTER'];
106 }
107
108 $instance = new static(static::getTargetEntityId());
109 $result = $instance->getList($order, $filter);
110
111 if (is_array($result))
112 {
113 unset($result['total']);
114 foreach ($result as $key => $item)
115 {
116 if (mb_strpos($item['FIELD_NAME'], static::$nameFullPrefix) !== 0)
117 {
118 unset($result[$key]);
119 }
120 }
121 $result = array_values($result);
122 $result['total'] = count($result);
123 }
124
125 return $result;
126 }
127
128 private static function checkFields(array $fields) : array
129 {
130 return array_intersect_key($fields, array_fill_keys(self::ALLOWED_FIELD_PROP_LIST, true));
131 }
132
133 private static function checkAccessField($fieldId)
134 {
135 $result = false;
136 if ($fieldId > 0)
137 {
138 $entity = new \CUserTypeEntity();
139 $res = $entity->getList(
140 [],
141 [
142 'ENTITY_ID' => static::getTargetEntityId(),
143 'ID' => $fieldId
144 ]
145 );
146
147 if ($field = $res->fetch())
148 {
149 if (mb_strpos($field['FIELD_NAME'], static::$nameFullPrefix) === 0)
150 {
151 $result = true;
152 }
153 }
154 }
155
156 return $result;
157 }
158}
static updateRest($query, $n, \CRestServer $server)
Definition userfield.php:50
static addRest($query, $n, \CRestServer $server)
Definition userfield.php:35
static deleteRest($query, $n, \CRestServer $server)
Definition userfield.php:75
static getListRest($query, $n, \CRestServer $server)
Definition userfield.php:94