/// <summary> /// Gets the week items for user. /// </summary> /// <param name="from">From.</param> /// <param name="to">To.</param> /// <param name="userId">The user id.</param> /// <returns></returns> public static WeekItemInfo[] GetWeekItemsForUser(DateTime from, DateTime to, int userId) { List <WeekItemInfo> retVal = new List <WeekItemInfo>(); // Load User TT blocks TimeTrackingBlock[] userBlocks = TimeTrackingBlock.List(FilterElement.EqualElement("OwnerId", userId), new IntervalFilterElement("StartDate", from, to)); // Sort Block By Start Date Array.Sort <TimeTrackingBlock>(userBlocks, CompareTimeTrackingBlockByStartDate); // Create Week Item Info list DateTime currentWeekStart = GetRealWeekStart(from); // TODO: Current while (currentWeekStart < to) { // Calculate aggregated status of all blocks WeekItemStatus status = CalculateWeekStatusByBlocks(currentWeekStart, userBlocks); WeekItemInfo item = new WeekItemInfo(currentWeekStart, Iso8601WeekNumber.GetWeekNumber(currentWeekStart.AddDays(3)), status); CalculateDayTotal(currentWeekStart, item, userBlocks); retVal.Add(item); // Go to next week currentWeekStart = currentWeekStart.AddDays(7); } return(retVal.ToArray()); }
public void Invoke(object Sender, object Element) { if (Element is CommandParameters) { CommandParameters cp = (CommandParameters)Element; DateTime startDate = DateTime.Parse(cp.CommandArguments["primaryKeyId"], CultureInfo.InvariantCulture); TimeTrackingBlock[] mas = TimeTrackingBlock.List(FilterElement.EqualElement("OwnerId", Mediachase.IBN.Business.Security.CurrentUser.UserID), FilterElement.EqualElement("StartDate", startDate)); using (TransactionScope tran = DataContext.Current.BeginTransaction()) { foreach (TimeTrackingBlock ttb in mas) { StateMachineService sms = ((BusinessObject)ttb).GetService <StateMachineService>(); // process only initial state if (sms.StateMachine.GetStateIndex(sms.CurrentState.Name) == 0) { StateTransition[] availableTransitions = sms.GetNextAvailableTransitions(true); if (availableTransitions.Length > 0) { sms.MakeTransition(availableTransitions[0].Uid); ttb.Save(); } } } tran.Commit(); } CHelper.RequireBindGrid(); } }
/// <summary> /// Gets the min start date. /// </summary> /// <param name="userId">The user id.</param> /// <returns></returns> public static DateTime GetTimeTrackingBlockMinStartDate(int userId) { TimeTrackingBlock[] userBlocks = TimeTrackingBlock.List( new FilterElementCollection(FilterElement.EqualElement("OwnerId", userId)), new SortingElementCollection(SortingElement.Ascending("StartDate")), 0, 1); if (userBlocks.Length > 0) { return(userBlocks[0].StartDate); } return(DateTime.Now.AddDays(-210)); }
public static bool CanUpdate(DateTime startDate, int projectId) { bool retval = false; if (Configuration.TimeTrackingModule) { startDate = GetWeekStart(startDate); // O.R. [2008-07-25] TimeTrackingBlockTypeInstance inst = null; using (SkipSecurityCheckScope scope = Mediachase.Ibn.Data.Services.Security.SkipSecurityCheck()) { inst = TimeTrackingManager.GetBlockTypeInstanceByProject(projectId); } if (inst != null) { TimeTrackingBlock[] blocks = TimeTrackingBlock.List( FilterElement.EqualElement("OwnerId", Security.CurrentUser.UserID), FilterElement.EqualElement("BlockTypeInstanceId", inst.PrimaryKeyId.Value), FilterElement.EqualElement("StartDate", startDate) ); if (blocks.Length > 0) // Block exists { TimeTrackingBlock block = blocks[0]; SecurityService ss = block.GetService <SecurityService>(); retval = ss.CheckUserRight(TimeTrackingManager.Right_Write); } else // Block doesn't exist { SecurityService ss = inst.GetService <SecurityService>(); retval = ss.CheckUserRight(TimeTrackingManager.Right_AddMyTTBlock) || ss.CheckUserRight(TimeTrackingManager.Right_AddAnyTTBlock); } } } return(retval); }
private void BindAllProjectsGrid(DateTime startDate, int userId) { DataTable dt = new DataTable(); dt.Columns.Add(new DataColumn("ObjectId", typeof(int))); dt.Columns.Add(new DataColumn("ObjectTypeId", typeof(int))); dt.Columns.Add(new DataColumn("ObjectName", typeof(string))); dt.Columns.Add(new DataColumn("BlockTypeInstanceId", typeof(int))); DataRow dr; #region 1. Make the list of all BlockTypeInstances Dictionary <int, string> allList = new Dictionary <int, string>(); bool isProject = false; foreach (ListItem li in BlockInstanceList.Items) { if (!isProject) { // Check that we have reached the [ All Projects ] if (li.Value == "0") { isProject = true; } continue; } allList.Add(int.Parse(li.Value, CultureInfo.InvariantCulture), li.Text); } #endregion #region 2. Get the list of the existing blocks by StartDate and OwnerId List <int> idList = new List <int>(); Dictionary <int, TimeTrackingBlock> existingBlocks = new Dictionary <int, TimeTrackingBlock>(); foreach (TimeTrackingBlock block in TimeTrackingBlock.List(FilterElement.EqualElement("StartDate", startDate), FilterElement.EqualElement("OwnerId", userId), FilterElement.IsNotNullElement("ProjectId"))) { idList.Add(block.PrimaryKeyId.Value); existingBlocks.Add(block.PrimaryKeyId.Value, block); } #endregion #region 3. Get the security info by existing blocks and remove the forbidden items from allList SerializableDictionary <int, Collection <string> > objectRights = Mediachase.Ibn.Data.Services.Security.GetAllowedRights(TimeTrackingBlock.GetAssignedMetaClass(), idList.ToArray()); foreach (KeyValuePair <int, Collection <string> > item in objectRights) { int id = item.Key; Collection <string> allowedRights = item.Value; TimeTrackingBlock block = existingBlocks[id]; if (!allowedRights.Contains(Mediachase.Ibn.Data.Services.Security.RightWrite)) { allList.Remove(block.BlockTypeInstanceId); } } #endregion #region 4. Fill in the DataTable foreach (KeyValuePair <int, string> item in allList) { bool isHeaderAdded = false; int instanceId = item.Key; string instanceName = item.Value; using (IDataReader reader = Mediachase.IBN.Business.TimeTracking.GetListTimeTrackingItemsForAdd(instanceId, startDate, userId)) { while (reader.Read()) { if (!isHeaderAdded) { dr = dt.NewRow(); dr["ObjectId"] = instanceId; dr["ObjectTypeId"] = ProjectObjectType; dr["ObjectName"] = instanceName; dr["BlockTypeInstanceId"] = instanceId; dt.Rows.Add(dr); isHeaderAdded = true; } dr = dt.NewRow(); dr["ObjectId"] = reader["ObjectId"]; dr["ObjectTypeId"] = reader["ObjectTypeId"]; dr["ObjectName"] = reader["ObjectName"]; dr["BlockTypeInstanceId"] = reader["BlockTypeInstanceId"]; dt.Rows.Add(dr); } } } #endregion MainGrid.DataSource = dt.DefaultView; MainGrid.DataBind(); }
private void BindUsersByBlockTypeInstance(int blockTypeInstanceId) { string savedValue; if (!IsPostBack) { savedValue = Mediachase.IBN.Business.Security.CurrentUser.UserID.ToString(); } else { savedValue = UserList.SelectedValue; } DateTime startDate = DTCWeek.SelectedDate; //if (startDate == DateTime.MinValue) // startDate = CHelper.GetWeekStartByDate(DTCWeek.Value); UserList.Items.Clear(); TimeTrackingBlockTypeInstance inst = MetaObjectActivator.CreateInstance <TimeTrackingBlockTypeInstance>(TimeTrackingBlockTypeInstance.GetAssignedMetaClass(), blockTypeInstanceId); if (Mediachase.Ibn.Data.Services.Security.CheckObjectRight(inst, TimeTrackingManager.Right_AddAnyTTBlock)) { #region 1. Make the Dictionary of Principal Dictionary <int, string> allUsers = new Dictionary <int, string>(); Principal[] principals = Principal.List(new FilterElementCollection(FilterElement.EqualElement("Card", "User"), FilterElement.EqualElement("Activity", 3)), new SortingElementCollection(new SortingElement("Name", SortingElementType.Asc))); foreach (Principal p in principals) { allUsers.Add(p.PrimaryKeyId.Value, p.Name); } #endregion #region 2. Make the list of the Id (to pass it as array) and the Dictionary of TimeTrackingBlock List <int> idList = new List <int>(); Dictionary <int, TimeTrackingBlock> allblocks = new Dictionary <int, TimeTrackingBlock>(); TimeTrackingBlock[] blocks = TimeTrackingBlock.List(FilterElement.EqualElement("StartDate", startDate), FilterElement.EqualElement("BlockTypeInstanceId", blockTypeInstanceId)); foreach (TimeTrackingBlock block in blocks) { idList.Add(block.PrimaryKeyId.Value); allblocks.Add(block.PrimaryKeyId.Value, block); } #endregion #region 3. Get the list of the existing blocks with rights and remove the forbidden items from allUsers SerializableDictionary <int, Collection <string> > objectRights = Mediachase.Ibn.Data.Services.Security.GetAllowedRights(TimeTrackingBlock.GetAssignedMetaClass(), idList.ToArray()); foreach (KeyValuePair <int, Collection <string> > item in objectRights) { int id = item.Key; Collection <string> allowedRights = item.Value; TimeTrackingBlock block = allblocks[id]; int ownerId = block.OwnerId; if (!allowedRights.Contains(Mediachase.Ibn.Data.Services.Security.RightWrite)) { allUsers.Remove(ownerId); } } #endregion #region 4. Fill in the dropdown foreach (KeyValuePair <int, string> item in allUsers) { UserList.Items.Add(new ListItem(item.Value, item.Key.ToString())); } #endregion } else { Mediachase.IBN.Business.UserLight usr = Mediachase.IBN.Business.Security.CurrentUser; UserList.Items.Add(new ListItem(usr.LastName + ", " + usr.FirstName, usr.UserID.ToString())); } if (savedValue != null) { CHelper.SafeSelect(UserList, savedValue); } }
private void BindUsers() { string savedValue; if (!IsPostBack) { savedValue = Mediachase.IBN.Business.Security.CurrentUser.UserID.ToString(); } else { savedValue = UserList.SelectedValue; } UserList.Items.Clear(); if (ProjectList.SelectedValue == null || startDate == DateTime.MinValue) { return; } int blockTypeInstanceId = int.Parse(ProjectList.SelectedValue, CultureInfo.InvariantCulture); TimeTrackingBlockTypeInstance inst = MetaObjectActivator.CreateInstance <TimeTrackingBlockTypeInstance>(TimeTrackingBlockTypeInstance.GetAssignedMetaClass(), blockTypeInstanceId); int userFromFilter = GetUserFromFilter(); if (userFromFilter > 0) { if (Mediachase.Ibn.Data.Services.Security.CheckObjectRight(inst, TimeTrackingManager.Right_AddAnyTTBlock) || (Mediachase.Ibn.Data.Services.Security.CheckObjectRight(inst, TimeTrackingManager.Right_AddMyTTBlock) && userFromFilter == Mediachase.Ibn.Data.Services.Security.CurrentUserId)) { TimeTrackingBlock block = TimeTrackingManager.GetTimeTrackingBlock(blockTypeInstanceId, startDate, userFromFilter); if (block == null || Mediachase.Ibn.Data.Services.Security.CanWrite(block)) { Principal pl = MetaObjectActivator.CreateInstance <Principal>(Principal.GetAssignedMetaClass(), userFromFilter); if (pl != null) { UserList.Items.Add(new ListItem(pl.Name, userFromFilter.ToString())); } } } } else // all users { if (Mediachase.Ibn.Data.Services.Security.CheckObjectRight(inst, TimeTrackingManager.Right_AddAnyTTBlock)) { #region 1. Make the Dictionary of Principal Dictionary <int, string> allUsers = new Dictionary <int, string>(); Principal[] principals = Principal.List(new FilterElementCollection(FilterElement.EqualElement("Card", "User"), FilterElement.EqualElement("Activity", 3)), new SortingElementCollection(new SortingElement("Name", SortingElementType.Asc))); foreach (Principal p in principals) { allUsers.Add(p.PrimaryKeyId.Value, p.Name); } #endregion #region 2. Make the list of the Id (to pass it as array) and the Dictionary of TimeTrackingBlock List <int> idList = new List <int>(); Dictionary <int, TimeTrackingBlock> allblocks = new Dictionary <int, TimeTrackingBlock>(); TimeTrackingBlock[] blocks = TimeTrackingBlock.List(FilterElement.EqualElement("StartDate", startDate), FilterElement.EqualElement("BlockTypeInstanceId", blockTypeInstanceId)); foreach (TimeTrackingBlock block in blocks) { idList.Add(block.PrimaryKeyId.Value); allblocks.Add(block.PrimaryKeyId.Value, block); } #endregion #region 3. Get the list of the existing blocks with rights and remove the forbidden items from allUsers SerializableDictionary <int, Collection <string> > objectRights = Mediachase.Ibn.Data.Services.Security.GetAllowedRights(TimeTrackingBlock.GetAssignedMetaClass(), idList.ToArray()); foreach (KeyValuePair <int, Collection <string> > item in objectRights) { int id = item.Key; Collection <string> allowedRights = item.Value; TimeTrackingBlock block = allblocks[id]; int ownerId = block.OwnerId; if (!allowedRights.Contains(Mediachase.Ibn.Data.Services.Security.RightWrite)) { allUsers.Remove(ownerId); } } #endregion #region 4. Fill in the dropdown foreach (KeyValuePair <int, string> item in allUsers) { UserList.Items.Add(new ListItem(item.Value, item.Key.ToString())); } #endregion } else if (Mediachase.Ibn.Data.Services.Security.CheckObjectRight(inst, TimeTrackingManager.Right_AddMyTTBlock)) { // eliminate the block for which we don't have the "Write" right TimeTrackingBlock block = TimeTrackingManager.GetTimeTrackingBlock(blockTypeInstanceId, startDate, Mediachase.Ibn.Data.Services.Security.CurrentUserId); if (block == null || Mediachase.Ibn.Data.Services.Security.CanWrite(block)) { Principal pl = MetaObjectActivator.CreateInstance <Principal>(Principal.GetAssignedMetaClass().Name, Mediachase.Ibn.Data.Services.Security.CurrentUserId); UserList.Items.Add(new ListItem(pl.Name, Mediachase.Ibn.Data.Services.Security.CurrentUserId.ToString())); } } } if (savedValue != null) { CHelper.SafeSelect(UserList, savedValue); } }
private void BindBlocks() { ProjectList.Items.Clear(); string titledFieldName = TimeTrackingManager.GetBlockTypeInstanceMetaClass().TitleFieldName; McMetaViewPreference pref = CHelper.GetMetaViewPreference(CurrentView); DateTime startDate = CHelper.GetRealWeekStartByDate(pref.GetAttribute <DateTime>(TTFilterPopupEdit.FilterWeekAttr, TTFilterPopupEdit.FilterWeekAttr, DateTime.MinValue)); if (startDate == DateTime.MinValue) { startDate = CHelper.GetRealWeekStartByDate(DateTime.Now); } int ownerId = Mediachase.IBN.Business.Security.CurrentUser.UserID; BindDayHeaders(startDate); // Non-project #region 1. Make the list of all BlockTypeInstances List <int> idList = new List <int>(); Dictionary <int, string> allList = new Dictionary <int, string>(); foreach (TimeTrackingBlockTypeInstance item in TimeTrackingManager.GetNonProjectBlockTypeInstances()) { idList.Add(item.PrimaryKeyId.Value); allList.Add(item.PrimaryKeyId.Value, item.Title); } #endregion #region 2. Check the rights AddMyTTBlock and AddAnyTTBlock and remove from the allList the forbidden items SerializableDictionary <int, Collection <string> > objectRights = Mediachase.Ibn.Data.Services.Security.GetAllowedRights(TimeTrackingBlockTypeInstance.GetAssignedMetaClass(), idList.ToArray()); foreach (KeyValuePair <int, Collection <string> > item in objectRights) { int id = item.Key; Collection <string> allowedRights = item.Value; if (!((allowedRights.Contains(TimeTrackingManager.Right_AddMyTTBlock) && ownerId == Mediachase.Ibn.Data.Services.Security.CurrentUserId) || allowedRights.Contains(TimeTrackingManager.Right_AddAnyTTBlock))) { allList.Remove(id); } } #endregion #region 3. Make the list of the TimeTrackingBlocks by OwnerId, StartDate and BlockTypeInstanceId[] List <string> blockTypeInstanceIdList = new List <string>(); foreach (int id in allList.Keys) { blockTypeInstanceIdList.Add(id.ToString(CultureInfo.InvariantCulture)); } List <int> blockIdList = new List <int>(); Dictionary <int, int> blockInstanceList = new Dictionary <int, int>(); TimeTrackingBlock[] blocks = TimeTrackingBlock.List( FilterElement.EqualElement("OwnerId", ownerId), FilterElement.EqualElement("StartDate", startDate), new FilterElement("BlockTypeInstanceId", FilterElementType.In, blockTypeInstanceIdList.ToArray()) ); foreach (TimeTrackingBlock block in blocks) { blockIdList.Add(block.PrimaryKeyId.Value); blockInstanceList.Add(block.PrimaryKeyId.Value, block.BlockTypeInstanceId); } #endregion #region 4. Check the right Write and remove from the allList the forbidden items objectRights = Mediachase.Ibn.Data.Services.Security.GetAllowedRights(TimeTrackingBlock.GetAssignedMetaClass(), blockIdList.ToArray()); foreach (KeyValuePair <int, Collection <string> > item in objectRights) { int id = item.Key; Collection <string> allowedRights = item.Value; if (!allowedRights.Contains(Mediachase.Ibn.Data.Services.Security.RightWrite)) { allList.Remove(blockInstanceList[id]); } } #endregion #region 5. Fill in the dropdown if (allList.Count > 0) { ProjectList.Items.Add(new ListItem(GetGlobalResourceObject("IbnFramework.TimeTracking", "ByActivity").ToString(), "-1")); foreach (KeyValuePair <int, string> item in allList) { ListItem li = new ListItem(" " + item.Value, item.Key.ToString()); ProjectList.Items.Add(li); } } #endregion // Projects #region 1. Make the list of all BlockTypeInstances idList = new List <int>(); allList = new Dictionary <int, string>(); foreach (TimeTrackingBlockTypeInstance item in TimeTrackingManager.GetProjectBlockTypeInstances()) { idList.Add(item.PrimaryKeyId.Value); allList.Add(item.PrimaryKeyId.Value, item.Title); } #endregion #region 2. Check the rights AddMyTTBlock and AddAnyTTBlock and remove from the allList the forbidden items objectRights = Mediachase.Ibn.Data.Services.Security.GetAllowedRights(TimeTrackingBlockTypeInstance.GetAssignedMetaClass(), idList.ToArray()); foreach (KeyValuePair <int, Collection <string> > item in objectRights) { int id = item.Key; Collection <string> allowedRights = item.Value; if (!((allowedRights.Contains(TimeTrackingManager.Right_AddMyTTBlock) && ownerId == Mediachase.Ibn.Data.Services.Security.CurrentUserId) || allowedRights.Contains(TimeTrackingManager.Right_AddAnyTTBlock))) { allList.Remove(id); } } #endregion #region 3. Make the list of the TimeTrackingBlocks by OwnerId, StartDate and BlockTypeInstanceId[] blockTypeInstanceIdList = new List <string>(); foreach (int id in allList.Keys) { blockTypeInstanceIdList.Add(id.ToString(CultureInfo.InvariantCulture)); } blockIdList = new List <int>(); blockInstanceList = new Dictionary <int, int>(); blocks = TimeTrackingBlock.List( FilterElement.EqualElement("OwnerId", ownerId), FilterElement.EqualElement("StartDate", startDate), new FilterElement("BlockTypeInstanceId", FilterElementType.In, blockTypeInstanceIdList.ToArray()) ); foreach (TimeTrackingBlock block in blocks) { blockIdList.Add(block.PrimaryKeyId.Value); blockInstanceList.Add(block.PrimaryKeyId.Value, block.BlockTypeInstanceId); } #endregion #region 4. Check the right Write and remove from the allList the forbidden items objectRights = Mediachase.Ibn.Data.Services.Security.GetAllowedRights(TimeTrackingBlock.GetAssignedMetaClass(), blockIdList.ToArray()); foreach (KeyValuePair <int, Collection <string> > item in objectRights) { int id = item.Key; Collection <string> allowedRights = item.Value; if (!allowedRights.Contains(Mediachase.Ibn.Data.Services.Security.RightWrite)) { allList.Remove(blockInstanceList[id]); } } #endregion #region 5. Fill in the dropdown if (allList.Count > 0) { ProjectList.Items.Add(new ListItem(GetGlobalResourceObject("IbnFramework.TimeTracking", "ByProject").ToString(), "-2")); foreach (KeyValuePair <int, string> item in allList) { ListItem li = new ListItem(" " + item.Value, item.Key.ToString()); ProjectList.Items.Add(li); } } #endregion if (blockInstanceId > 0) { CHelper.SafeSelect(ProjectList, blockInstanceId.ToString()); } EnsureSelectInstance(); }