github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/parsers/tree/revoke.go (about) 1 // Copyright 2021 Matrix Origin 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package tree 16 17 import ( 18 "fmt" 19 20 "github.com/matrixorigin/matrixone/pkg/sql/parsers/dialect" 21 ) 22 23 type RevokeType int 24 25 const ( 26 RevokeTypePrivilege RevokeType = iota 27 RevokeTypeRole 28 ) 29 30 type Revoke struct { 31 statementImpl 32 Typ RevokeType 33 RevokePrivilege RevokePrivilege 34 RevokeRole RevokeRole 35 } 36 37 func (node *Revoke) Format(ctx *FmtCtx) { 38 switch node.Typ { 39 case RevokeTypePrivilege: 40 node.RevokePrivilege.Format(ctx) 41 case RevokeTypeRole: 42 node.RevokeRole.Format(ctx) 43 } 44 } 45 func (node *Revoke) GetStatementType() string { return "Revoke" } 46 func (node *Revoke) GetQueryType() string { return QueryTypeDCL } 47 48 type RevokePrivilege struct { 49 statementImpl 50 IfExists bool 51 Privileges []*Privilege 52 ObjType ObjectType 53 Level *PrivilegeLevel 54 Roles []*Role 55 } 56 57 func (node *RevokePrivilege) Format(ctx *FmtCtx) { 58 ctx.WriteString("revoke") 59 if node.IfExists { 60 ctx.WriteString(" if exists") 61 } 62 63 if node.Privileges != nil { 64 prefix := " " 65 for _, p := range node.Privileges { 66 ctx.WriteString(prefix) 67 p.Format(ctx) 68 prefix = ", " 69 } 70 } 71 ctx.WriteString(" on") 72 if node.ObjType != OBJECT_TYPE_NONE { 73 ctx.WriteByte(' ') 74 ctx.WriteString(node.ObjType.String()) 75 } 76 if node.Level != nil { 77 ctx.WriteByte(' ') 78 node.Level.Format(ctx) 79 } 80 81 if node.Roles != nil { 82 ctx.WriteString(" from") 83 prefix := " " 84 for _, r := range node.Roles { 85 ctx.WriteString(prefix) 86 r.Format(ctx) 87 prefix = ", " 88 } 89 } 90 } 91 92 func (node *RevokePrivilege) GetStatementType() string { return "Revoke Privilege" } 93 func (node *RevokePrivilege) GetQueryType() string { return QueryTypeDCL } 94 95 func NewRevoke() *Revoke { 96 return &Revoke{} 97 } 98 99 type RevokeRole struct { 100 statementImpl 101 IfExists bool 102 Roles []*Role 103 Users []*User 104 } 105 106 func (node *RevokeRole) Format(ctx *FmtCtx) { 107 ctx.WriteString("revoke") 108 if node.IfExists { 109 ctx.WriteString(" if exists") 110 } 111 if node.Roles != nil { 112 prefix := " " 113 for _, r := range node.Roles { 114 ctx.WriteString(prefix) 115 r.Format(ctx) 116 prefix = ", " 117 } 118 } 119 if node.Users != nil { 120 ctx.WriteString(" from") 121 prefix := " " 122 for _, r := range node.Users { 123 ctx.WriteString(prefix) 124 r.Format(ctx) 125 prefix = ", " 126 } 127 } 128 } 129 130 func (node *RevokeRole) GetStatementType() string { return "Revoke Role" } 131 func (node *RevokeRole) GetQueryType() string { return QueryTypeDCL } 132 133 type PrivilegeLevel struct { 134 NodeFormatter 135 Level PrivilegeLevelType 136 DbName string 137 TabName string 138 RoutineName string 139 } 140 141 func (node *PrivilegeLevel) Format(ctx *FmtCtx) { 142 switch node.Level { 143 case PRIVILEGE_LEVEL_TYPE_STAR: 144 ctx.WriteString("*") 145 case PRIVILEGE_LEVEL_TYPE_STAR_STAR: 146 ctx.WriteString("*.*") 147 case PRIVILEGE_LEVEL_TYPE_DATABASE_STAR: 148 ctx.WriteString(fmt.Sprintf("%s.*", node.DbName)) 149 case PRIVILEGE_LEVEL_TYPE_DATABASE_TABLE: 150 ctx.WriteString(fmt.Sprintf("%s.%s", node.DbName, node.TabName)) 151 case PRIVILEGE_LEVEL_TYPE_TABLE: 152 ctx.WriteString(node.TabName) 153 } 154 } 155 156 func (node *PrivilegeLevel) String() string { 157 fmtCtx := NewFmtCtx(dialect.MYSQL) 158 node.Format(fmtCtx) 159 return fmtCtx.String() 160 } 161 162 func NewPrivilegeLevel(l PrivilegeLevelType, d, t, r string) *PrivilegeLevel { 163 return &PrivilegeLevel{ 164 Level: l, 165 DbName: d, 166 TabName: t, 167 RoutineName: r, 168 } 169 } 170 171 type PrivilegeLevelType int 172 173 const ( 174 PRIVILEGE_LEVEL_TYPE_STAR PrivilegeLevelType = iota //* 175 PRIVILEGE_LEVEL_TYPE_STAR_STAR //*.* 176 PRIVILEGE_LEVEL_TYPE_DATABASE //db_name 177 PRIVILEGE_LEVEL_TYPE_DATABASE_STAR //db_name.* 178 PRIVILEGE_LEVEL_TYPE_DATABASE_TABLE //db_name.tbl_name 179 PRIVILEGE_LEVEL_TYPE_TABLE //tbl_name 180 PRIVILEGE_LEVEL_TYPE_COLUMN // (x,x) 181 PRIVILEGE_LEVEL_TYPE_STORED_ROUTINE //procedure 182 PRIVILEGE_LEVEL_TYPE_PROXY 183 PRIVILEGE_LEVEL_TYPE_ROUTINE 184 ) 185 186 type Privilege struct { 187 NodeFormatter 188 Type PrivilegeType 189 ColumnList []*UnresolvedName 190 } 191 192 func (node *Privilege) Format(ctx *FmtCtx) { 193 ctx.WriteString(node.Type.ToString()) 194 if node.ColumnList != nil { 195 prefix := "(" 196 for _, c := range node.ColumnList { 197 ctx.WriteString(prefix) 198 c.Format(ctx) 199 prefix = ", " 200 } 201 ctx.WriteByte(')') 202 } 203 } 204 205 func NewPrivilege(t PrivilegeType, c []*UnresolvedName) *Privilege { 206 return &Privilege{ 207 Type: t, 208 ColumnList: c, 209 } 210 } 211 212 type ObjectType int 213 214 func (node *ObjectType) String() string { 215 switch *node { 216 case OBJECT_TYPE_TABLE: 217 return "table" 218 case OBJECT_TYPE_FUNCTION: 219 return "function" 220 case OBJECT_TYPE_PROCEDURE: 221 return "procedure" 222 case OBJECT_TYPE_ACCOUNT: 223 return "account" 224 case OBJECT_TYPE_DATABASE: 225 return "database" 226 default: 227 return "Unknown ObjectType" 228 } 229 } 230 231 const ( 232 OBJECT_TYPE_NONE ObjectType = iota 233 OBJECT_TYPE_TABLE 234 OBJECT_TYPE_DATABASE 235 OBJECT_TYPE_FUNCTION 236 OBJECT_TYPE_PROCEDURE 237 OBJECT_TYPE_VIEW 238 OBJECT_TYPE_ACCOUNT 239 ) 240 241 type PrivilegeType int 242 243 func (node *PrivilegeType) ToString() string { 244 switch *node { 245 case PRIVILEGE_TYPE_STATIC_ALL: 246 return "all" 247 case PRIVILEGE_TYPE_STATIC_ALTER: 248 return "alter" 249 case PRIVILEGE_TYPE_STATIC_ALTER_ROUTINE: 250 return "alter routine" 251 case PRIVILEGE_TYPE_STATIC_CREATE: 252 return "create" 253 case PRIVILEGE_TYPE_STATIC_CREATE_ROLE: 254 return "create role" 255 case PRIVILEGE_TYPE_STATIC_CREATE_ROUTINE: 256 return "create routine" 257 case PRIVILEGE_TYPE_STATIC_CREATE_TABLESPACE: 258 return "create tablespace" 259 case PRIVILEGE_TYPE_STATIC_CREATE_TEMPORARY_TABLES: 260 return "temporary tables" 261 case PRIVILEGE_TYPE_STATIC_CREATE_USER: 262 return "create user" 263 case PRIVILEGE_TYPE_STATIC_CREATE_VIEW: 264 return "create view" 265 case PRIVILEGE_TYPE_STATIC_DELETE: 266 return "delete" 267 case PRIVILEGE_TYPE_STATIC_DROP: 268 return "drop" 269 case PRIVILEGE_TYPE_STATIC_DROP_ROLE: 270 return "drop role" 271 case PRIVILEGE_TYPE_STATIC_EVENT: 272 return "event" 273 case PRIVILEGE_TYPE_STATIC_EXECUTE: 274 return "execute" 275 case PRIVILEGE_TYPE_STATIC_FILE: 276 return "file" 277 case PRIVILEGE_TYPE_STATIC_GRANT_OPTION: 278 return "grant option" 279 case PRIVILEGE_TYPE_STATIC_INDEX: 280 return "index" 281 case PRIVILEGE_TYPE_STATIC_INSERT: 282 return "insert" 283 case PRIVILEGE_TYPE_STATIC_LOCK_TABLES: 284 return "lock tables" 285 case PRIVILEGE_TYPE_STATIC_PROCESS: 286 return "process" 287 case PRIVILEGE_TYPE_STATIC_PROXY: 288 return "proxy" 289 case PRIVILEGE_TYPE_STATIC_REFERENCES: 290 return "reference" 291 case PRIVILEGE_TYPE_STATIC_RELOAD: 292 return "reload" 293 case PRIVILEGE_TYPE_STATIC_REPLICATION_CLIENT: 294 return "replication client" 295 case PRIVILEGE_TYPE_STATIC_REPLICATION_SLAVE: 296 return "replication slave" 297 case PRIVILEGE_TYPE_STATIC_SELECT: 298 return "select" 299 case PRIVILEGE_TYPE_STATIC_SHOW_DATABASES: 300 return "show databases" 301 case PRIVILEGE_TYPE_STATIC_SHOW_VIEW: 302 return "show view" 303 case PRIVILEGE_TYPE_STATIC_SHUTDOWN: 304 return "shutdown" 305 case PRIVILEGE_TYPE_STATIC_SUPER: 306 return "super" 307 case PRIVILEGE_TYPE_STATIC_TRIGGER: 308 return "trigger" 309 case PRIVILEGE_TYPE_STATIC_UPDATE: 310 return "update" 311 case PRIVILEGE_TYPE_STATIC_USAGE: 312 return "usage" 313 case PRIVILEGE_TYPE_STATIC_CONNECT: 314 return "connect" 315 case PRIVILEGE_TYPE_STATIC_OWNERSHIP: 316 return "ownership" 317 case PRIVILEGE_TYPE_STATIC_MANAGE_GRANTS: 318 return "manage grants" 319 case PRIVILEGE_TYPE_STATIC_TRUNCATE: 320 return "truncate" 321 case PRIVILEGE_TYPE_STATIC_REFERENCE: 322 return "reference" 323 default: 324 return "Unknown PrivilegeType" 325 } 326 } 327 328 /* 329 * 330 From: https://dev.mysql.com/doc/refman/8.0/en/grant.html 331 */ 332 const ( 333 PRIVILEGE_TYPE_STATIC_ALL PrivilegeType = iota //Grant all privileges at specified access level except GRANT OPTION and PROXY. 334 PRIVILEGE_TYPE_STATIC_CREATE_ACCOUNT 335 PRIVILEGE_TYPE_STATIC_DROP_ACCOUNT 336 PRIVILEGE_TYPE_STATIC_ALTER_ACCOUNT 337 PRIVILEGE_TYPE_STATIC_UPGRADE_ACCOUNT 338 PRIVILEGE_TYPE_STATIC_CREATE_USER //Enable use of CREATE USER, DROP USER, RENAME USER, and REVOKE ALL PRIVILEGES. Level: Global. 339 PRIVILEGE_TYPE_STATIC_DROP_USER 340 PRIVILEGE_TYPE_STATIC_ALTER_USER 341 PRIVILEGE_TYPE_STATIC_CREATE_ROLE //Enable role creation. Level: Global. 342 PRIVILEGE_TYPE_STATIC_DROP_ROLE //Enable roles to be dropped. Level: Global. 343 PRIVILEGE_TYPE_STATIC_ALTER_ROLE 344 PRIVILEGE_TYPE_STATIC_CREATE_DATABASE 345 PRIVILEGE_TYPE_STATIC_DROP_DATABASE 346 PRIVILEGE_TYPE_STATIC_SHOW_DATABASES //Enable SHOW DATABASES to show all databases. Level: Global. 347 PRIVILEGE_TYPE_STATIC_CONNECT 348 PRIVILEGE_TYPE_STATIC_MANAGE_GRANTS 349 PRIVILEGE_TYPE_STATIC_OWNERSHIP 350 PRIVILEGE_TYPE_STATIC_SHOW_TABLES 351 PRIVILEGE_TYPE_STATIC_CREATE_TABLE 352 PRIVILEGE_TYPE_STATIC_DROP_TABLE 353 PRIVILEGE_TYPE_STATIC_DROP_VIEW 354 PRIVILEGE_TYPE_STATIC_ALTER_TABLE 355 PRIVILEGE_TYPE_STATIC_ALTER_VIEW 356 PRIVILEGE_TYPE_STATIC_SELECT //Enable use of SELECT. Levels: Global, database, table, column. 357 PRIVILEGE_TYPE_STATIC_INSERT //Enable use of INSERT. Levels: Global, database, table, column. 358 PRIVILEGE_TYPE_STATIC_TRUNCATE //Enable use of REPLACE. Levels: Global, database, table, column. 359 PRIVILEGE_TYPE_STATIC_UPDATE //Enable use of UPDATE. Levels: Global, database, table, column. 360 PRIVILEGE_TYPE_STATIC_DELETE //Enable use of DELETE. Level: Global, database, table. 361 PRIVILEGE_TYPE_STATIC_REFERENCES //Enable foreign key creation. Levels: Global, database, table, column. 362 PRIVILEGE_TYPE_STATIC_REFERENCE 363 PRIVILEGE_TYPE_STATIC_INDEX //Enable indexes to be created or dropped. Levels: Global, database, table. 364 PRIVILEGE_TYPE_STATIC_EXECUTE //Enable the user to execute stored routines. Levels: Global, database, routine. 365 PRIVILEGE_TYPE_STATIC_VALUES //Enable use of VALUES. Levels: Global, database, table. 366 367 PRIVILEGE_TYPE_STATIC_ALTER 368 PRIVILEGE_TYPE_STATIC_CREATE 369 PRIVILEGE_TYPE_STATIC_DROP 370 PRIVILEGE_TYPE_STATIC_ALTER_ROUTINE //Enable stored routines to be altered or dropped. Levels: Global, database, routine. 371 PRIVILEGE_TYPE_STATIC_CREATE_ROUTINE //Enable stored routine creation. Levels: Global, database. 372 PRIVILEGE_TYPE_STATIC_CREATE_TABLESPACE //Enable tablespaces and log file groups to be created, altered, or dropped. Level: Global. 373 PRIVILEGE_TYPE_STATIC_CREATE_TEMPORARY_TABLES //Enable use of CREATE TEMPORARY TABLE. Levels: Global, database. 374 PRIVILEGE_TYPE_STATIC_CREATE_VIEW //Enable views to be created or altered. Levels: Global, database, table. 375 PRIVILEGE_TYPE_STATIC_EVENT //Enable use of events for the Event Scheduler. Levels: Global, database. 376 PRIVILEGE_TYPE_STATIC_FILE //Enable the user to cause the server to read or write files. Level: Global. 377 PRIVILEGE_TYPE_STATIC_GRANT_OPTION //Enable privileges to be granted to or removed from other accounts. Levels: Global, database, table, routine, proxy. 378 PRIVILEGE_TYPE_STATIC_LOCK_TABLES //Enable use of LOCK TABLES on tables for which you have the SELECT privilege. Levels: Global, database. 379 PRIVILEGE_TYPE_STATIC_PROCESS //Enable the user to see all processes with SHOW PROCESSLIST. Level: Global. 380 PRIVILEGE_TYPE_STATIC_PROXY //Enable user proxying. Level: From user to user. 381 PRIVILEGE_TYPE_STATIC_RELOAD //Enable use of FLUSH operations. Level: Global. 382 PRIVILEGE_TYPE_STATIC_REPLICATION_CLIENT //Enable the user to ask where source or replica servers are. Level: Global. 383 PRIVILEGE_TYPE_STATIC_REPLICATION_SLAVE //Enable replicas to read binary log events from the source. Level: Global. 384 PRIVILEGE_TYPE_STATIC_SHOW_VIEW //Enable use of SHOW CREATE VIEW. Levels: Global, database, table. 385 PRIVILEGE_TYPE_STATIC_SHUTDOWN //Enable use of mysqladmin shutdown. Level: Global. 386 PRIVILEGE_TYPE_STATIC_SUPER //Enable use of other administrative operations such as CHANGE REPLICATION SOURCE TO, CHANGE MASTER TO, KILL, PURGE BINARY LOGS, SET GLOBAL, and mysqladmin debug command. Level: Global. 387 PRIVILEGE_TYPE_STATIC_TRIGGER //Enable trigger operations. Levels: Global, database, table. 388 PRIVILEGE_TYPE_STATIC_USAGE //Synonym for “no privileges” 389 PRIVILEGE_TYPE_ 390 PRIVILEGE_TYPE_DYNAMIC_APPLICATION_PASSWORD_ADMIN //Enable dual password administration. Level: Global. 391 PRIVILEGE_TYPE_DYNAMIC_AUDIT_ADMIN //Enable audit log configuration. Level: Global. 392 PRIVILEGE_TYPE_DYNAMIC_BACKUP_ADMIN //Enable backup administration. Level: Global. 393 PRIVILEGE_TYPE_DYNAMIC_BINLOG_ADMIN //Enable binary log control. Level: Global. 394 PRIVILEGE_TYPE_DYNAMIC_BINLOG_ENCRYPTION_ADMIN //Enable activation and deactivation of binary log encryption. Level: Global. 395 PRIVILEGE_TYPE_DYNAMIC_CLONE_ADMIN //Enable clone administration. Level: Global. 396 PRIVILEGE_TYPE_DYNAMIC_CONNECTION_ADMIN //Enable connection limit/restriction control. Level: Global. 397 PRIVILEGE_TYPE_DYNAMIC_ENCRYPTION_KEY_ADMIN //Enable InnoDB key rotation. Level: Global. 398 PRIVILEGE_TYPE_DYNAMIC_FIREWALL_ADMIN //Enable firewall rule administration, any user. Level: Global. 399 PRIVILEGE_TYPE_DYNAMIC_FIREWALL_EXEMPT //Exempt user from firewall restrictions. Level: Global. 400 PRIVILEGE_TYPE_DYNAMIC_FIREWALL_USER //Enable firewall rule administration, self. Level: Global. 401 PRIVILEGE_TYPE_DYNAMIC_FLUSH_OPTIMIZER_COSTS //Enable optimizer cost reloading. Level: Global. 402 PRIVILEGE_TYPE_DYNAMIC_FLUSH_STATUS //Enable status indicator flushing. Level: Global. 403 PRIVILEGE_TYPE_DYNAMIC_FLUSH_TABLES //Enable table flushing. Level: Global. 404 PRIVILEGE_TYPE_DYNAMIC_FLUSH_USER_RESOURCES //Enable user-resource flushing. Level: Global. 405 PRIVILEGE_TYPE_DYNAMIC_GROUP_REPLICATION_ADMIN //Enable Group Replication control. Level: Global. 406 PRIVILEGE_TYPE_DYNAMIC_INNODB_REDO_LOG_Enable //Enable or disable redo logging. Level: Global. 407 PRIVILEGE_TYPE_DYNAMIC_INNODB_REDO_LOG_ARCHIVE //Enable redo log archiving administration. Level: Global. 408 PRIVILEGE_TYPE_DYNAMIC_NDB_STORED_USER //Enable sharing of user or role between SQL nodes (NDB Cluster). Level: Global. 409 PRIVILEGE_TYPE_DYNAMIC_PERSIST_RO_VARIABLES_ADMIN //Enable persisting read-only system variables. Level: Global. 410 PRIVILEGE_TYPE_DYNAMIC_REPLICATION_APPLIER //Act as the PRIVILEGE_CHECKS_USER for a replication channel. Level: Global. 411 PRIVILEGE_TYPE_DYNAMIC_REPLICATION_SLAVE_ADMIN //Enable regular replication control. Level: Global. 412 PRIVILEGE_TYPE_DYNAMIC_RESOURCE_GROUP_ADMIN //Enable resource group administration. Level: Global. 413 PRIVILEGE_TYPE_DYNAMIC_RESOURCE_GROUP_USER //Enable resource group administration. Level: Global. 414 PRIVILEGE_TYPE_DYNAMIC_ROLE_ADMIN //Enable roles to be granted or revoked, use of WITH ADMIN OPTION. Level: Global. 415 PRIVILEGE_TYPE_DYNAMIC_SESSION_VARIABLES_ADMIN //Enable setting restricted session system variables. Level: Global. 416 PRIVILEGE_TYPE_DYNAMIC_SET_USER_ID //Enable setting non-self DEFINER values. Level: Global. 417 PRIVILEGE_TYPE_DYNAMIC_SHOW_ROUTINE //Enable access to stored routine definitions. Level: Global. 418 PRIVILEGE_TYPE_DYNAMIC_SYSTEM_USER //Designate account as system account. Level: Global. 419 PRIVILEGE_TYPE_DYNAMIC_SYSTEM_VARIABLES_ADMIN //Enable modifying or persisting global system variables. Level: Global. 420 PRIVILEGE_TYPE_DYNAMIC_TABLE_ENCRYPTION_ADMIN //Enable overriding default encryption settings. Level: Global. 421 PRIVILEGE_TYPE_DYNAMIC_VERSION_TOKEN_ADMIN //Enable use of Version Tokens functions. Level: Global. 422 PRIVILEGE_TYPE_DYNAMIC_XA_RECOVER_ADMIN //Enable XA RECOVER execution. Level: Global. 423 )