Step 4: Create Default Admin for the Repository
- Last UpdatedJun 10, 2024
- 3 minute read
The following example demonstrates the creation of default admin for the repository using API.
Create default admin for the repository:
PROCEDURE
DLL REFERENCE
Skelta.CentralConfigCodeBehind.dll
Skelta.Forms.Core.dll
NAMESPACE USED:
Skelta.Forms.Core
Skelta.Forms.Core.Controls
Skelta.Repository.List
string repositoryName = "Sample Repository";
Skelta.Core.ApplicationObject applicationObject = new Skelta.Core.ApplicationObject(repositoryName);
//Create ListDefintion object
Skelta.Repository.List.ListDefinition ld = new Skelta.Repository.List.ListDefinition(applicationObject, "Security Group Mapping");
//Create ListDatahandler object
Skelta.Repository.List.ListDataHandler ldh = new Skelta.Repository.List.ListDataHandler(repositoryName, "Security Group");
Guid SGID = ldh.GetListItemId("Administrator"); //Get Administrator SecurityGroup Id
bool IsalreadyMapped = false; //flag variable to check whether user already configured or not.
Skelta.Repository.Security.SecurityLevel _SecurityLevel = Skelta.Repository.Security.SecurityLevel.Repository;
string _ApplicationName = repositoryName;
//Using entity infra to get the admin user from entity data source provider.
Skelta.Entity.Entity userEntity = Skelta.Entity.Entity.GetEntity(applicationObject, "UserEntity");
object[] adminRealId = ((Skelta.Entity.IEntityAdminDataSourceProvider)userEntity.DataBridge.DataBridgeProviders["sqlproviderCustom"]).GetAdministrator();
//Change the provider user details
Skelta.HWS.Actor actr = new Skelta.HWS.Actor(applicationObject, "sqlproviderCustom::" + adminRealId[0]);
string UserId = actr.Id.ToString();
string UserName = actr.Resource.Properties.Name.Value.ToString();
//Create SecurityRightsHandler Object
Skelta.Repository.Security.SecurityRightsHandler srh = new Skelta.Repository.Security.SecurityRightsHandler(repositoryName);
Workflow.NET.Interfaces.IDataHandler dbhandler = Workflow.NET.Storage.DataHandlerFactory.GetDataHandler(ld.Configuration);
IDataParameter[] parametres = new IDataParameter[3];
IDataReader rdr = null;
using (dbhandler)
{
parametres[0] = dbhandler.GetParameter("@ApplicationName", _ApplicationName);
parametres[1] = dbhandler.GetParameter("@SGId", SGID);
parametres[2] = dbhandler.GetParameter("@UserId", new Guid(UserId));
string strQuery = "Select * from " + ld.TableName + " Where Application=@ApplicationName AND SecurityGroupId=@SGId AND UserId= @UserId";
rdr = dbhandler.ExecuteReader(strQuery, parametres);
if (rdr.Read())
{
if (rdr[0] != System.DBNull.Value)
IsalreadyMapped = true;
}
}
if (rdr != null)
rdr.Close();
if (!IsalreadyMapped) //If user not configured as admin
{
using (TransactionScope TScope = Workflow.NET.CommonFunctions.GetNewTransactionScope())
{
Skelta.Repository.List.ListItem li = new Skelta.Repository.List.ListItem(ld);
ListMainForm listTableForm = ((ListMainForm)li.ListForm.Records[0].FindControlByID("_sys_sgm_form"));
((ListTextDataItem)listTableForm.FindControlByID("_sys_sgm_sgtitle")).Value = _SecurityLevel.ToString(); //"Administrator";
((ListGuidDataItem)listTableForm.FindControlByID("_sys_sgm_sgid")).Value = SGID;
((ListGuidDataItem)listTableForm.FindControlByID("_sys_sgm_userid")).Value = UserId;
((ListGuidDataItem)listTableForm.FindControlByID("_sys_sgm_roleid")).Value = Guid.Empty;
((ListIntDataItem)listTableForm.FindControlByID("_sys_sgm_priority")).Value = 0;
((ListGuidDataItem)listTableForm.FindControlByID("_sys_sgm_mappedlistId")).Value = Guid.Empty;
((ListGuidDataItem)listTableForm.FindControlByID("_sys_sgm_mappedlistitemid")).Value = Guid.Empty;
((ListGuidDataItem)listTableForm.FindControlByID("_sys_sgm_effective_security_id")).Value = Guid.Empty; ((ListGuidDataItem)listTableForm.FindControlByID("_sys_sgm_security_customization_id")).Value = Guid.Empty;
li.Save(ListItemVersionStatus.Published);
TScope.Complete();
}
Skelta.CentralConfigCodeBehind.CommonFunctions commonFunctions = new Skelta.CentralConfigCodeBehind.CommonFunctions();
commonFunctions.UpdateRespositoryPropetyCollection(repositoryName);
dbhandler = DataHandlerFactory.GetDataHandler(ld.Configuration);
using (dbhandler)
{
IDataParameter[] param = new IDataParameter[2];
param[0] = dbhandler.GetParameter("@UserId", new Guid(UserId));
param[1] = dbhandler.GetParameter("@Application", (DbType)System.Data.SqlDbType.VarChar, 50, ParameterDirection.Input, repositoryName);
dbhandler.ExecuteProcedure("USP_Update_UserId", param);
}
}
Verification:
The above code created an Admin user with complete rights for the repository. The above code will also do an entry in SKVirtualActor table. The code would execute entity data source provider methods to get the required user information. Use this query to check the user entry in virtual actor table. select * from SKVirtualActor Where Name='Michelle'. The user named Michelle can now login to the EnterpriseConsole site using the email Id (since loginproperty for provider is set as Email) and check the menu.
-
Additional API to validate the API Entries:
API to validate if entry already exists for Data Source, Repository and AddInProvider is listed below.
/// <summary>
/// To check if the data source to be created already exists
/// </summary>
/// <param name="DataSourceName">Name of the data source to be checked</param>
/// <returns></returns>
public static bool IsDataSourceExists(string DataSourceName)
{
Log SkeltaLog = new Log();
try
{
DataSourceCollection dsc = new DataSourceCollection();
if (dsc.Contains(DataSourceName))
{
return true;
}
else
{
SkeltaLog.LogInformation("Skelta on " + System.Net.Dns.GetHostName() + " does not contain " + DataSourceName + ". Unable to find datasource.");
return false;
}
}
catch (Exception exception)
{
SkeltaLog.LogError(exception, "Error while getting repository details");
return false;
}
}
/// <summary>
/// To check if the repository exists. Refer Skelta Log for Exceptions if any
/// </summary>
/// <param name="Repository"></param>
/// <returns>True if exists else false. false even on exceptions</returns>
public static bool IsRepositoryExist(string Repository)
{
Log SkeltaLog = new Log();
try
{
RepositoryCollection rc = new RepositoryCollection();
if (rc.Contains(Repository))
{
return true;
}
else
{
return false;
}
}
catch (Exception exception)
{
SkeltaLog.LogError(exception, "Error while getting repository details");
return false;
}
}
/// <summary>
/// To check if the Provider with the given name for the repository exists
/// </summary>
/// <param name="RepositoryName">Name of the repository for which the configuration needs to be added</param>
/// <param name="AddInProviderName">Name of the Add In Provider to be checked</param>
/// <returns>True if exists else false</returns>
public static bool IsProviderExists(string RepositoryName, string AddInProviderName)
{
Log SkeltaLog = new Log();
try
{
ApplicationObject applicationObject = new ApplicationObject(RepositoryName);
AddInProviderCollection skAddInProviderCollection = new AddInProviderCollection(applicationObject);
return skAddInProviderCollection.Contains(AddInProviderName);
}
catch (Exception exception)
{
SkeltaLog.LogError(exception, "Error while getting provider details");
throw;
}
}