//--------------------------------------------------------------------------------------------------------------------- /// <summary>Calculates whether a user has the specified privilege for the specified domain.</summary> /// <param name="context">The execution environment context.</param> /// <param name="userId">The database ID of the user in question.</param> /// <param name="domainId">The database ID of the domain in question. If the domain is <em>null<em>, the check is.</param> /// <param name="identifier">The unique identifier of the privilege.</param> /// <returns><i>true</i> if the user has the privilege.</returns> private static bool DoesUserHavePrivilegeInternal(IfyContext context, int userId, int domainId, string condition) { StringBuilder sql = new StringBuilder(); sql.Append("SELECT "); sql.Append(PrivilegeValueSelectSql); sql.Append(" FROM "); sql.Append(PrivilegeBaseJoinSql); sql.Replace("{0}", userId.ToString()); sql.Append(" WHERE "); sql.Append(String.Format("(rg.id_usr={0} OR ug.id_usr={0}) AND ", userId)); sql.Append(domainId == 0 ? "rg.id_domain IS NULL" : String.Format("(rg.id_domain IS NULL OR rg.id_domain={0})", domainId)); if (condition != null) { sql.Append(String.Format(" AND {0};", condition)); } IDbConnection dbConnection = context.GetDbConnection(); IDataReader reader = context.GetQueryResult(sql.ToString(), dbConnection); bool result = reader.Read(); context.CloseQueryResult(reader, dbConnection); return(result); }
//--------------------------------------------------------------------------------------------------------------------- /// <summary>Executes the background agent action <b>Computing Element status refresh</b>.</summary> /// <param name="context">The execution environment context.</param> /// <remarks> /// <p>This method is called periodically if the action is active and the background agent is running according to the action's execution interval specified in the portal configuration.</p> /// <p>The background agent action <b>Computing Element status refresh</b> refreshes task, job and node status information for active tasks.</p> /// </remarks> public static void ExecuteComputingResourceStatusRefresh(IfyContext context) { IDataReader reader; ComputingResource computingResource; List <int> computingResourceIds = new List <int>(); int unknownCount = 0; // Add computing resources into status table if not yet there context.Execute("INSERT INTO crstate (id_cr) SELECT id FROM cr AS t LEFT JOIN crstate AS t1 ON t.id=t1.id_cr WHERE t1.id_cr IS NULL;"); // Get computing resources with actual status Active (10) reader = null; string sql = "SELECT t.id FROM ce AS t WHERE status_method IS NOT NULL ORDER BY t.id;"; // WHERE status=" + ProcessingStatus.Active + " AND remote_id IS NOT NULL ORDER BY t.id;"; IDbConnection dbConnection = context.GetDbConnection(); reader = context.GetQueryResult(sql, dbConnection); while (reader.Read()) { computingResourceIds.Add(reader.GetInt32(0)); } context.CloseQueryResult(reader, dbConnection); //context.WriteInfo(String.Format("Active tasks: {0}{1}", computingResourceIds.Count, computingResourceIds.Count == 0 ? String.Empty : " - status display format: created/active/failed/incomplete/completed")); // Get available computing resource information for (int i = 0; i < computingResourceIds.Count; i++) { computingResource = ComputingResource.FromId(context, computingResourceIds[i]); //computingResource.Restricted = false; computingResource.GetStatus(); if (computingResource.Refreshed) { context.WriteInfo( String.Format("{0}: total: {1}, free: {2}", computingResource.Name, computingResource.TotalCapacity, computingResource.FreeCapacity ) ); } else { unknownCount++; } } if (unknownCount != 0) { context.WriteWarning(String.Format("Computing resources with unavailable status: {0} (out of {1})", unknownCount, computingResourceIds.Count)); } computingResourceIds.Clear(); }
//--------------------------------------------------------------------------------------------------------------------- public List <int> GetTaskIdList(IfyContext context) { List <int> taskIds = new List <int>(); string sql = String.Format("SELECT t.id FROM task AS t WHERE id_scheduler={0};", this.Id); IDbConnection dbConnection = context.GetDbConnection(); IDataReader reader = context.GetQueryResult(sql, dbConnection); while (reader.Read()) { taskIds.Add(reader.GetInt32(0)); } context.CloseQueryResult(reader, dbConnection); return(taskIds); }
//--------------------------------------------------------------------------------------------------------------------- /// <summary>Determines the scope (global or domain-restricted) for which the specified user or any of the specified groups has been granted at least one of the specified roles.</summary> /// <returns> /// <para>The database IDs of the domains for which the user has at least one of the roles. The code using this method for privilege-based authorisation checks, has to distinguish the following cases:</para> /// <list type="bullet"> /// <item>An empty array means that the user is not authorised.</item> /// <item>An array containing one or more IDs means that the user is authorised for items that belong to the domains with these database IDs.</item> /// <item>If the array is <c>null</c>, the user is authorised globally.</item> /// </list> /// </returns> /// <param name="context">The execution environment context.</param> /// <param name="userId">The database ID of the user for which the domain restriction check is performed. This can be used in combination with <em>groupIds</em>. If no specific user is to be taken into account, the value <em>0</em> can be provided.</param> /// <param name="groupIds">The database IDs of the groups for which the domain restriction check is performed. This can be used in combination with <em>userId</em>. If no group is to be taken into account, <em>null</em> can be used.</param> /// <param name="roleIds">An array of database IDs for the roles that are to be checked in relation to the user. If the array is <c>null</c>, the method returns all domains on which the user or groups have any role. If the array is empty, the grant is empty (return value is an empty array).</param> public static int[] GetGrantScope(IfyContext context, int userId, int[] groupIds, int[] roleIds) { if (roleIds != null && roleIds.Length == 0) { return new int[] {} } ; if (groupIds == null || groupIds.Length == 0) { groupIds = new int[] { 0 } } ; string roleCondition = roleIds == null ? String.Empty : String.Format("rg.id_role IN ({0})", String.Join(",", roleIds)); List <int> domainIds = new List <int>(); string sql = String.Format("SELECT DISTINCT rg.id_domain FROM rolegrant AS rg LEFT JOIN usr_grp AS ug ON rg.id_grp=ug.id_grp WHERE {2}{3}(rg.id_usr={0} OR ug.id_usr={0} OR rg.id_grp IN ({1})) ORDER BY rg.id_domain IS NULL, rg.id_domain;", userId, String.Join(",", groupIds), roleCondition, String.IsNullOrEmpty(roleCondition) ? String.Empty : " AND " ); //Console.WriteLine("DOMAINS: {0}", sql); IDbConnection dbConnection = context.GetDbConnection(); IDataReader reader = context.GetQueryResult(sql, dbConnection); bool globallyAuthorized = false; while (reader.Read()) { // The domain ID NULL means that the user has the privilege globally and other any additional domains do not matter // This applies only if a role was specifically queried, but not if all domains on which a user has any role are queried. if (roleIds != null && reader.GetValue(0) == DBNull.Value) { globallyAuthorized = true; break; } domainIds.Add(reader.GetInt32(0)); } context.CloseQueryResult(reader, dbConnection); if (globallyAuthorized) { return(null); } return(domainIds.ToArray()); }
//--------------------------------------------------------------------------------------------------------------------- public static RemoteResourceEntityCollection GetResources(IfyContext context, RemoteResourceSet resourceSet) { RemoteResourceEntityCollection result = new RemoteResourceEntityCollection(context); result.Template.ResourceSet = resourceSet; IDbConnection dbConnection = context.GetDbConnection(); IDataReader reader = context.GetQueryResult(String.Format("SELECT t.id, t.location, t.name FROM resource AS t WHERE id_set={0}", resourceSet.Id), dbConnection); while (reader.Read()) { RemoteResource resource = new RemoteResource(context); resource.Id = reader.GetInt32(0); resource.ResourceSet = resourceSet; resource.Location = reader.GetString(1); resource.Name = reader.GetString(2); } context.CloseQueryResult(reader, dbConnection); return(result); }
/*****************************************************************************************************************/ /// <summary>Creates a new DBCookie instance representing the cookie with the specified session ID and unique identifier.</summary> /// <param name="context">The execution environment context.</param> /// <param name="session">The related session ID.</param> /// <param name="identifier">The unique identifier of the cookie.</param> /// <returns>The created Group object.</returns> public static DBCookie FromSessionAndIdentifier(IfyContext context, string session, string identifier) { DBCookie cookie = new DBCookie(context); string sql = String.Format("SELECT value, expire, creation_date FROM cookie WHERE session={0} AND identifier={1};", StringUtils.EscapeSql(session), StringUtils.EscapeSql(identifier)); IDbConnection dbConnection = context.GetDbConnection(); IDataReader reader = context.GetQueryResult(sql, dbConnection); Console.WriteLine(sql); if (reader.Read()) { cookie.Session = session; cookie.Identifier = identifier; cookie.Value = reader.GetString(0); cookie.Expire = reader.GetDateTime(1); cookie.CreationDate = reader.GetDateTime(2); } context.CloseQueryResult(reader, dbConnection); return(cookie); }
//--------------------------------------------------------------------------------------------------------------------- /// <summary> /// Gets the task list. /// </summary> /// <returns>The task list.</returns> /// <param name="context">Context.</param> /// \xrefitem rmodp "RM-ODP" "RM-ODP Documentation" public List <Task> GetTaskList(IfyContext context) { List <int> taskIds = new List <int>(); IDbConnection dbConnection = context.GetDbConnection(); string sql = String.Format("SELECT t.id FROM task AS t WHERE id_scheduler={0};", this.Id); IDataReader reader = context.GetQueryResult(sql, dbConnection); while (reader.Read()) { taskIds.Add(reader.GetInt32(0)); } context.CloseQueryResult(reader, dbConnection); List <Task> result = new List <Task>(); foreach (int taskId in taskIds) { result.Add(Task.FromId(context, taskId, null, true)); } return(result); }
//--------------------------------------------------------------------------------------------------------------------- /// <summary> /// Gets the user roles for a specific domain. /// </summary> /// <returns>The user roles for domain.</returns> /// <param name="context">Context.</param> /// <param name="userId">User identifier.</param> /// <param name="domainId">Domain identifier.</param> public static Role[] GetUserRolesForDomain(IfyContext context, int userId, int domainId) { List <Role> result = new List <Role> (); string sql = String.Format("SELECT id_role FROM rolegrant WHERE id_usr={0} AND id_domain={1};", userId, domainId); List <int> rolesId = new List <int> (); IDbConnection dbConnection = context.GetDbConnection(); IDataReader reader = context.GetQueryResult(sql, dbConnection); while (reader.Read()) { rolesId.Add(reader.GetInt32(0)); } context.CloseQueryResult(reader, dbConnection); foreach (var id in rolesId) { result.Add(Role.FromId(context, id)); } return(result.ToArray()); }
//--------------------------------------------------------------------------------------------------------------------- /// <summary>Returns the privileges of a user for the specified entity item.</summary> /// <param name="context">The execution environment context.</param> /// <param name="userId">The database ID of the user in question.</param> /// <param name="item">The entity item for which the privileges are calculated.</param> /// <returns>An array of the privileges related to the entity type that contains only those privileges granted to the user for the item.</returns> public static Privilege[] GetUserPrivileges(IfyContext context, int userId, Entity item) { StringBuilder sql = new StringBuilder(); sql.Append("SELECT DISTINCT p.id FROM "); sql.Append(PrivilegeBaseJoinSql); sql.Replace("{0}", userId.ToString()); sql.Append(String.Format(" WHERE p.id_type={0} AND ", item.EntityType.TopTypeId)); sql.Append(String.Format("(rg.id_usr={0} OR ug.id_usr={0}) AND ", userId)); sql.Append(item.DomainId == 0 ? "rg.id_domain IS NULL" : String.Format("(rg.id_domain IS NULL OR rg.id_domain={0});", item.DomainId)); List <Privilege> result = new List <Privilege>(); IDbConnection dbConnection = context.GetDbConnection(); IDataReader reader = context.GetQueryResult(sql.ToString(), dbConnection); while (reader.Read()) { result.Add(Privilege.Get(reader.GetInt32(0))); } context.CloseQueryResult(reader, dbConnection); return(result.ToArray()); }