Skip to content

Commit

Permalink
Add granted by in DDL of privilege
Browse files Browse the repository at this point in the history
Also closes #40
  • Loading branch information
arvanus committed Oct 25, 2023
1 parent 1001daa commit df9002a
Show file tree
Hide file tree
Showing 18 changed files with 60 additions and 41 deletions.
16 changes: 8 additions & 8 deletions src/metadata/MetadataTemplateCmdHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,21 +300,21 @@ void MetadataTemplateCmdHandler::handleTemplateCmd(TemplateProcessor *tp,
Domain* dom = dynamic_cast<Domain*>(object);
std::vector<Privilege>* p = 0;
if (rel)
p = rel->getPrivileges();
p = rel->getPrivileges(false);
if (proc)
p = proc->getPrivileges();
p = proc->getPrivileges(false);
if (role)
p = role->getPrivileges();
p = role->getPrivileges(false);
if (func)
p = func->getPrivileges();
p = func->getPrivileges(false);
if (pack)
p = pack->getPrivileges();
p = pack->getPrivileges(false);
if (gen)
p = gen->getPrivileges();
p = gen->getPrivileges(false);
if (exc)
p = exc->getPrivileges();
p = exc->getPrivileges(false);
if (dom)
p = dom->getPrivileges();
p = dom->getPrivileges(false);
if (!p)
return;
bool firstItem = true;
Expand Down
8 changes: 5 additions & 3 deletions src/metadata/domain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ void Domain::acceptVisitor(MetadataItemVisitor* visitor)
visitor->visitDomain(*this);
}

std::vector<Privilege>* Domain::getPrivileges()
std::vector<Privilege>* Domain::getPrivileges(bool splitPerGrantor)
{
// load privileges from database and return the pointer to collection
DatabasePtr db = getDatabase();
Expand All @@ -427,11 +427,12 @@ std::vector<Privilege>* Domain::getPrivileges()
"RDB$GRANT_OPTION, RDB$FIELD_NAME "
"from RDB$USER_PRIVILEGES "
"where RDB$RELATION_NAME = ? and rdb$object_type = 9 "
"order by rdb$user, rdb$user_type, rdb$grant_option, rdb$privilege"
"order by rdb$user, rdb$user_type, rdb$grantor, rdb$grant_option, rdb$privilege"
);
st1->Set(1, wx2std(getName_(), converter));
st1->Execute();
std::string lastuser;
std::string lastGrantor;
int lasttype = -1;
Privilege* pr = 0;
while (st1->Fetch())
Expand All @@ -445,12 +446,13 @@ std::vector<Privilege>* Domain::getPrivileges()
if (!st1->IsNull(5))
st1->Get(5, grantoption);
st1->Get(6, field);
if (!pr || user != lastuser || usertype != lasttype)
if (!pr || user != lastuser || usertype != lasttype || (splitPerGrantor && grantor != lastGrantor))
{
Privilege p(this, wxString(user.c_str(), *converter).Strip(), usertype);
privilegesM.push_back(p);
pr = &privilegesM.back();
lastuser = user;
lastGrantor = grantor;
lasttype = usertype;
}
pr->addPrivilege(privilege[0], std2wxIdentifier(grantor, converter),
Expand Down
2 changes: 1 addition & 1 deletion src/metadata/domain.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class Domain: public MetadataItem
wxString getAlterSqlTemplate() const;
virtual const wxString getTypeName() const;
virtual void acceptVisitor(MetadataItemVisitor* v);
std::vector<Privilege>* getPrivileges();
std::vector<Privilege>* getPrivileges(bool splitPerGrantor=true);
};

class DomainCollectionBase: public MetadataCollection<Domain>
Expand Down
8 changes: 5 additions & 3 deletions src/metadata/exception.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ void Exception::acceptVisitor(MetadataItemVisitor* visitor)
visitor->visitException(*this);
}

std::vector<Privilege>* Exception::getPrivileges()
std::vector<Privilege>* Exception::getPrivileges(bool splitPerGrantor)
{
// load privileges from database and return the pointer to collection
DatabasePtr db = getDatabase();
Expand All @@ -151,11 +151,12 @@ std::vector<Privilege>* Exception::getPrivileges()
"RDB$GRANT_OPTION, RDB$FIELD_NAME "
"from RDB$USER_PRIVILEGES "
"where RDB$RELATION_NAME = ? and rdb$object_type = 7 "
"order by rdb$user, rdb$user_type, rdb$grant_option, rdb$privilege"
"order by rdb$user, rdb$user_type, rdb$grantor, rdb$grant_option, rdb$privilege"
);
st1->Set(1, wx2std(getName_(), converter));
st1->Execute();
std::string lastuser;
std::string lastGrantor;
int lasttype = -1;
Privilege* pr = 0;
while (st1->Fetch())
Expand All @@ -169,12 +170,13 @@ std::vector<Privilege>* Exception::getPrivileges()
if (!st1->IsNull(5))
st1->Get(5, grantoption);
st1->Get(6, field);
if (!pr || user != lastuser || usertype != lasttype)
if (!pr || user != lastuser || usertype != lasttype || (splitPerGrantor && grantor != lastGrantor))
{
Privilege p(this, wxString(user.c_str(), *converter).Strip(), usertype);
privilegesM.push_back(p);
pr = &privilegesM.back();
lastuser = user;
lastGrantor = grantor;
lasttype = usertype;
}
pr->addPrivilege(privilege[0], std2wxIdentifier(grantor, converter),
Expand Down
2 changes: 1 addition & 1 deletion src/metadata/exception.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class Exception: public MetadataItem

virtual const wxString getTypeName() const;
virtual void acceptVisitor(MetadataItemVisitor* visitor);
std::vector<Privilege>* getPrivileges();
std::vector<Privilege>* getPrivileges(bool splitPerGrantor=true);
};

class Exceptions : public MetadataCollection<Exception>
Expand Down
8 changes: 5 additions & 3 deletions src/metadata/function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ wxString Function::getSqlSecurity()
}
}

std::vector<Privilege>* Function::getPrivileges()
std::vector<Privilege>* Function::getPrivileges(bool splitPerGrantor)
{
// load privileges from database and return the pointer to collection
DatabasePtr db = getDatabase();
Expand All @@ -334,11 +334,12 @@ std::vector<Privilege>* Function::getPrivileges()
"RDB$GRANT_OPTION, RDB$FIELD_NAME "
"from RDB$USER_PRIVILEGES "
"where RDB$RELATION_NAME = ? and rdb$object_type = 15 "
"order by rdb$user, rdb$user_type, rdb$privilege"
"order by rdb$user, rdb$user_type, rdb$grantor, rdb$privilege"
);
st1->Set(1, wx2std(getName_(), converter));
st1->Execute();
std::string lastuser;
std::string lastGrantor;
int lasttype = -1;
Privilege* pr = 0;
while (st1->Fetch())
Expand All @@ -352,13 +353,14 @@ std::vector<Privilege>* Function::getPrivileges()
if (!st1->IsNull(5))
st1->Get(5, grantoption);
st1->Get(6, field);
if (!pr || user != lastuser || usertype != lasttype)
if (!pr || user != lastuser || usertype != lasttype || (splitPerGrantor && grantor != lastGrantor))
{
Privilege p(this, std2wxIdentifier(user, converter),
usertype);
privilegesM.push_back(p);
pr = &privilegesM.back();
lastuser = user;
lastGrantor = grantor;
lasttype = usertype;
}
pr->addPrivilege(privilege[0], wxString(grantor.c_str(), *converter),
Expand Down
2 changes: 1 addition & 1 deletion src/metadata/function.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class Function: public MetadataItem
virtual wxString getSource() = 0;
wxString getSqlSecurity();
virtual const wxString getTypeName() const = 0;
std::vector<Privilege>* getPrivileges();
std::vector<Privilege>* getPrivileges(bool splitPerGrantor=true);

virtual void acceptVisitor(MetadataItemVisitor* visitor);
virtual void checkDependentFunction();
Expand Down
8 changes: 5 additions & 3 deletions src/metadata/generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ wxString Generator::getSource()
return sql ;
}

std::vector<Privilege>* Generator::getPrivileges()
std::vector<Privilege>* Generator::getPrivileges(bool splitPerGrantor)
{
// load privileges from database and return the pointer to collection
DatabasePtr db = getDatabase();
Expand All @@ -137,11 +137,12 @@ std::vector<Privilege>* Generator::getPrivileges()
"RDB$GRANT_OPTION, RDB$FIELD_NAME "
"from RDB$USER_PRIVILEGES "
"where RDB$RELATION_NAME = ? and rdb$object_type = 14 "
"order by rdb$user, rdb$user_type, rdb$grant_option, rdb$privilege"
"order by rdb$user, rdb$user_type, rdb$grantor, rdb$grant_option, rdb$privilege"
);
st1->Set(1, wx2std(getName_(), converter));
st1->Execute();
std::string lastuser;
std::string lastGrantor;
int lasttype = -1;
Privilege* pr = 0;
while (st1->Fetch())
Expand All @@ -155,12 +156,13 @@ std::vector<Privilege>* Generator::getPrivileges()
if (!st1->IsNull(5))
st1->Get(5, grantoption);
st1->Get(6, field);
if (!pr || user != lastuser || usertype != lasttype)
if (!pr || user != lastuser || usertype != lasttype || (splitPerGrantor && grantor != lastGrantor))
{
Privilege p(this, wxString(user.c_str(), *converter).Strip(), usertype);
privilegesM.push_back(p);
pr = &privilegesM.back();
lastuser = user;
lastGrantor = grantor;
lasttype = usertype;
}
pr->addPrivilege(privilege[0], std2wxIdentifier(grantor, converter),
Expand Down
2 changes: 1 addition & 1 deletion src/metadata/generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class Generator: public MetadataItem
virtual const wxString getTypeName() const;
virtual void acceptVisitor(MetadataItemVisitor* visitor);
wxString getSource();
std::vector<Privilege>* getPrivileges();
std::vector<Privilege>* getPrivileges(bool splitPerGrantor=true);
};

class Generators: public MetadataCollection<Generator>
Expand Down
8 changes: 5 additions & 3 deletions src/metadata/package.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ void Package::checkDependentPackage()
}
}

std::vector<Privilege>* Package::getPrivileges()
std::vector<Privilege>* Package::getPrivileges(bool splitPerGrantor)
{
// load privileges from database and return the pointer to collection
DatabasePtr db = getDatabase();
Expand All @@ -432,11 +432,12 @@ std::vector<Privilege>* Package::getPrivileges()
"RDB$GRANT_OPTION, RDB$FIELD_NAME "
"from RDB$USER_PRIVILEGES "
"where RDB$RELATION_NAME = ? and rdb$object_type in( 18, 19 ) "
"order by rdb$user, rdb$user_type, rdb$privilege"
"order by rdb$user, rdb$user_type, rdb$grantor, rdb$privilege"
);
st1->Set(1, wx2std(getName_(), converter));
st1->Execute();
std::string lastuser;
std::string lastGrantor;
int lasttype = -1;
Privilege *pr = 0;
while (st1->Fetch())
Expand All @@ -450,13 +451,14 @@ std::vector<Privilege>* Package::getPrivileges()
if (!st1->IsNull(5))
st1->Get(5, grantoption);
st1->Get(6, field);
if (!pr || user != lastuser || usertype != lasttype)
if (!pr || user != lastuser || usertype != lasttype || (splitPerGrantor && grantor != lastGrantor))
{
Privilege p(this, std2wxIdentifier(user, converter),
usertype);
privilegesM.push_back(p);
pr = &privilegesM.back();
lastuser = user;
lastGrantor = grantor;
lasttype = usertype;
}
pr->addPrivilege(privilege[0], wxString(grantor.c_str(), *converter),
Expand Down
2 changes: 1 addition & 1 deletion src/metadata/package.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class Package : public MetadataItem
wxString getAlterHeader();
wxString getAlterBody();

std::vector<Privilege>* getPrivileges();
std::vector<Privilege>* getPrivileges(bool splitPerGrantor=true);

void checkDependentPackage();

Expand Down
5 changes: 4 additions & 1 deletion src/metadata/privilege.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,11 @@ wxString Privilege::getSql(bool withGrantOption) const
Identifier id(granteeM);
ret += parentObjectM->getQuotedName() + " TO "
+ granteeTypeToString(granteeTypeM) + " " + id.getQuoted();

if (withGrantOption)
ret += " WITH GRANT OPTION";

ret += " GRANTED BY "+ (privilegeItemsM.begin())->second.grantor;
ret += ";\n";
return ret;
}
Expand All @@ -173,6 +175,7 @@ wxString Privilege::getSql() const
break;
}
}
ret += " GRANTED BY " + (privilegeItemsM.begin())->second.grantor;
ret += ";\n";
return ret;
}
Expand Down
8 changes: 5 additions & 3 deletions src/metadata/procedure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ void Procedure::checkDependentProcedures()
}
}

std::vector<Privilege>* Procedure::getPrivileges()
std::vector<Privilege>* Procedure::getPrivileges(bool splitPerGrantor)
{
// load privileges from database and return the pointer to collection
DatabasePtr db = getDatabase();
Expand All @@ -517,11 +517,12 @@ std::vector<Privilege>* Procedure::getPrivileges()
"RDB$GRANT_OPTION, RDB$FIELD_NAME "
"from RDB$USER_PRIVILEGES "
"where RDB$RELATION_NAME = ? and rdb$object_type = 5 "
"order by rdb$user, rdb$user_type, rdb$privilege"
"order by rdb$user, rdb$user_type, rdb$grantor, rdb$privilege"
);
st1->Set(1, wx2std(getName_(), converter));
st1->Execute();
std::string lastuser;
std::string lastGrantor;
int lasttype = -1;
Privilege *pr = 0;
while (st1->Fetch())
Expand All @@ -535,13 +536,14 @@ std::vector<Privilege>* Procedure::getPrivileges()
if (!st1->IsNull(5))
st1->Get(5, grantoption);
st1->Get(6, field);
if (!pr || user != lastuser || usertype != lasttype)
if (!pr || user != lastuser || usertype != lasttype || (splitPerGrantor && grantor != lastGrantor))
{
Privilege p(this, std2wxIdentifier(user, converter),
usertype);
privilegesM.push_back(p);
pr = &privilegesM.back();
lastuser = user;
lastGrantor = grantor;
lasttype = usertype;
}
pr->addPrivilege(privilege[0], wxString(grantor.c_str(), *converter),
Expand Down
2 changes: 1 addition & 1 deletion src/metadata/procedure.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class Procedure : public MetadataItem
wxString getDefinition(); // used for calltip in sql editor
wxString getSqlSecurity();

std::vector<Privilege>* getPrivileges();
std::vector<Privilege>* getPrivileges(bool splitPerGrantor=true);

void checkDependentProcedures();

Expand Down
8 changes: 5 additions & 3 deletions src/metadata/relation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ wxString Relation::getRebuildSql(const wxString& forColumn)
return sql;
}

std::vector<Privilege>* Relation::getPrivileges()
std::vector<Privilege>* Relation::getPrivileges(bool splitPerGrantor)
{
// load privileges from database and return the pointer to collection
DatabasePtr db = getDatabase();
Expand All @@ -610,11 +610,12 @@ std::vector<Privilege>* Relation::getPrivileges()
"RDB$GRANT_OPTION, RDB$FIELD_NAME "
"from RDB$USER_PRIVILEGES "
"where RDB$RELATION_NAME = ? and rdb$object_type = 0 "
"order by rdb$user, rdb$user_type, rdb$grant_option, rdb$privilege"
"order by rdb$user, rdb$user_type, rdb$grantor, rdb$grant_option, rdb$privilege"
);
st1->Set(1, wx2std(getName_(), converter));
st1->Execute();
std::string lastuser;
std::string lastGrantor;
int lasttype = -1;
Privilege *pr = 0;
while (st1->Fetch())
Expand All @@ -628,12 +629,13 @@ std::vector<Privilege>* Relation::getPrivileges()
if (!st1->IsNull(5))
st1->Get(5, grantoption);
st1->Get(6, field);
if (!pr || user != lastuser || usertype != lasttype)
if (!pr || user != lastuser || usertype != lasttype || (splitPerGrantor && grantor != lastGrantor))
{
Privilege p(this, wxString(user.c_str(), *converter).Strip(), usertype);
privilegesM.push_back(p);
pr = &privilegesM.back();
lastuser = user;
lastGrantor = grantor;
lasttype = usertype;
}
pr->addPrivilege(privilege[0], std2wxIdentifier(grantor, converter),
Expand Down
2 changes: 1 addition & 1 deletion src/metadata/relation.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class Relation: public MetadataItem
ColumnPtr findColumn(const wxString& name) const;

wxString getRebuildSql(const wxString& forColumn = "");
std::vector<Privilege>* getPrivileges();
std::vector<Privilege>* getPrivileges(bool splitPerGrantor=true);
bool getChildren(std::vector<MetadataItem *>& temp);
void getTriggers(std::vector<Trigger*>& list,
Trigger::FiringTime time);
Expand Down
Loading

0 comments on commit df9002a

Please sign in to comment.