/// <summary>
        /// Test ID 024
        /// Sample permissions for administrative statuses
        /// </summary>
        /// <param name="dataset"></param>
        public static void AssignPermissions(PhysicalInstance dataset)
        {
            // Get the Repository API
            var api = GetRepositoryApiWindows();

            // create a new item permission for the survey
            var permission = new ItemPermission();

            permission.Permission = PermissionType.ExclusiveWrite;
            permission.RoleName   = "*****@*****.**";
            //permission.RoleName = "yourdomain\\username";
            permission.Identifier = dataset.Identifier;
            permission.AgencyId   = dataset.AgencyId;

            // apply the permission on the Repository
            var roles = new RepositorySecurityContext();

            roles.IdentifierPermissions.Add(permission);
            api.AddPermissions(roles);
        }
        public void InitView(long idLink, long idItem, long idVersion, long idFolder, String path, Boolean setBackUrl, String backUrl)
        {
            RepositoryIdentifier rIdentifier = Service.ItemGetRepositoryIdentifier(idItem);
            Int32 idCommunity = UserContext.CurrentCommunityID;

            View.IdItem                      = idItem;
            View.IdVersion                   = idVersion;
            View.IdLink                      = idLink;
            View.IdCurrentFolder             = idFolder;
            View.CurrentFolderIdentifierPath = path;
            if (setBackUrl && String.IsNullOrEmpty(backUrl))
            {
                backUrl = View.GetPreviousRelativeUrl();
                if (!IsValidPreviousUrl(backUrl))
                {
                    backUrl    = "";
                    setBackUrl = false;
                }
                if (backUrl.StartsWith(RootObject.BaseRepositoryUrl(), StringComparison.InvariantCultureIgnoreCase) && rIdentifier != null)
                {
                    cookieRepository cookie = View.GetRepositoryCookie(rIdentifier);
                    if (cookie != null)
                    {
                        backUrl = RootObject.RepositoryItems(rIdentifier.Type, rIdentifier.IdCommunity, 0, cookie.IdFolder, cookie.Type, cookie.ItemsOrderBy, cookie.Ascending, cookie.IdentifierPath);
                    }
                }
                SetLogoutUrl(View.GetCurrentUrl(), setBackUrl, backUrl);
            }
            else
            {
                View.DefaultLogoutUrl = View.GetCurrentUrl();
            }
            View.BackUrl = (setBackUrl ? backUrl : "");
            View.SetPageBackUrl(backUrl);
            View.PageIdentifier = Guid.NewGuid();
            if (SessionTimeout())
            {
                return;
            }

            View.IsInitialized = true;
            ModuleRepository.ActionType uAction = ModuleRepository.ActionType.None;

            if (rIdentifier == null)
            {
                uAction = ModuleRepository.ActionType.MultimedaSettingsTryToLoad;
                View.DisplayUnknownItem();
            }
            else
            {
                Int32 idCurrentUser               = UserContext.CurrentUserID;
                liteRepositoryItem        item    = Service.ItemGet(idItem);
                liteRepositoryItemVersion version = Service.ItemGetVersion(idItem, idVersion);
                if (version == null)
                {
                    uAction = ModuleRepository.ActionType.MultimedaSettingsTryToLoad;
                    View.DisplayUnknownItem();
                }
                else if (version.Type != ItemType.Multimedia)
                {
                    idCommunity = version.IdCommunity;
                    View.DisplayMessage(version.DisplayName, version.Extension, version.Type, Domain.UserMessageType.multimediaSettingsInvalidType);
                    uAction = ModuleRepository.ActionType.MultimedaSettingsInvalidType;
                }
                else
                {
                    idVersion      = version.Id;
                    View.IdVersion = idVersion;
                    idCommunity    = version.IdCommunity;
                    if (version.Availability == ItemAvailability.available || version.Availability == ItemAvailability.waitingsettings)
                    {
                        ItemPermission permissions = null;

                        if (idLink == 0)
                        {
                            dtoDisplayRepositoryItem dto = Service.GetItemWithPermissions(idItem, idCurrentUser, item.Repository, View.GetUnknownUserName());
                            if (dto != null)
                            {
                                permissions = dto.Permissions;
                            }
                        }
                        else
                        {
                            liteModuleLink link = CurrentManager.Get <liteModuleLink>(idLink);
                            if (link == null)
                            {
                                View.DisplayMessage(version.DisplayName, version.Extension, version.Type, Domain.UserMessageType.multimediaSettingsNoPermission);
                            }
                            else
                            {
                                permissions = View.GetLinkPermissions(link, idCurrentUser);
                                idCommunity = link.SourceItem.CommunityID;
                            }
                        }
                        if (permissions != null)
                        {
                            if (permissions.EditSettings)
                            {
                                if (version.Availability == ItemAvailability.waitingsettings)
                                {
                                    View.DisplayMessage(version.DisplayName, version.Extension, version.Type, Domain.UserMessageType.multimediaSettingsNoDefaultDocument);
                                }
                                List <dtoMultimediaFileObject> items = Service.MultimediaGetItems(idItem, idVersion);
                                View.AllowSave = (items != null && items.Any());
                                View.LoadItems(item.UniqueIdVersion.ToString(), item.DisplayName, items, (items == null ? null : items.Where(i => i.IsDefaultDocument).FirstOrDefault()));
                            }
                            else
                            {
                                uAction = ModuleRepository.ActionType.MultimedaSettingsNoPermissions;
                                View.DisplayMessage(version.DisplayName, version.Extension, version.Type, Domain.UserMessageType.multimediaSettingsNoPermission);
                            }
                        }
                        else
                        {
                            uAction = ModuleRepository.ActionType.MultimedaSettingsStatusError;
                            View.DisplayMessage(version.DisplayName, version.Extension, version.Type, Domain.UserMessageType.multimediaSettingsInvalidStatus, version.Availability);
                        }
                    }
                    else
                    {
                        uAction = ModuleRepository.ActionType.MultimedaSettingsStatusError;
                        View.DisplayMessage(version.DisplayName, version.Extension, version.Type, Domain.UserMessageType.multimediaSettingsInvalidStatus, version.Availability);
                    }
                }
                View.RepositoryIdentifier  = CacheKeys.RepositoryIdentifierKey(rIdentifier.Type, rIdentifier.IdCommunity);
                View.RepositoryIdCommunity = rIdentifier.IdCommunity;
                View.RepositoryType        = rIdentifier.Type;
                View.IsInitialized         = true;
            }
            View.SendUserAction(idCommunity, Service.GetIdModule(), uAction);
        }
 private Expression <Func <TItem, bool> > GetPermissionPredicate(ItemPermission permission)
 {
     return(_stem.PermissionExpression?.Chain(p => (p & permission) != 0));
     //return _stem.PermissionExpression?.Chain(p => p.HasFlag(permission)); // supported in EF6.1 apparently?
 }
        public void InitView(long idSettings, long idLink, long idItem, long idVersion)
        {
            RepositoryIdentifier rIdentifier = Service.ItemGetRepositoryIdentifier(idItem);
            Int32 idCommunity = UserContext.CurrentCommunityID;

            View.IdItem         = idItem;
            View.IdVersion      = idVersion;
            View.IdLink         = idLink;
            View.PageIdentifier = Guid.NewGuid();
            if (SessionTimeout())
            {
                return;
            }

            View.IsInitialized = true;
            ModuleRepository.ActionType uAction = ModuleRepository.ActionType.None;

            if (rIdentifier == null)
            {
                uAction = ModuleRepository.ActionType.ScormSettingsTryToLoad;
                View.DisplayUnknownItem();
            }
            else
            {
                Int32 idCurrentUser               = UserContext.CurrentUserID;
                liteRepositoryItem        item    = Service.ItemGet(idItem);
                liteRepositoryItemVersion version = Service.ItemGetVersion(idItem, idVersion);
                if (version == null)
                {
                    uAction = ModuleRepository.ActionType.ScormSettingsTryToLoad;
                    View.DisplayUnknownItem();
                }
                else if (version.Type != ItemType.ScormPackage)
                {
                    idCommunity = version.IdCommunity;
                    View.DisplayMessage(version.DisplayName, version.Extension, version.Type, Domain.UserMessageType.scormSettingsInvalidType);
                    uAction = ModuleRepository.ActionType.ScormSettingsInvalidType;
                }
                else
                {
                    idVersion      = version.Id;
                    View.IdVersion = idVersion;
                    idCommunity    = version.IdCommunity;
                    if (version.Availability == ItemAvailability.available || version.Availability == ItemAvailability.waitingsettings)
                    {
                        ItemPermission permissions = null;

                        if (idLink == 0)
                        {
                            dtoDisplayRepositoryItem dto = Service.GetItemWithPermissions(idItem, idCurrentUser, item.Repository, View.GetUnknownUserName());
                            if (dto != null)
                            {
                                permissions = dto.Permissions;
                            }
                        }
                        else
                        {
                            liteModuleLink link = CurrentManager.Get <liteModuleLink>(idLink);
                            if (link == null)
                            {
                                View.DisplayMessage(version.DisplayName, version.Extension, version.Type, Domain.UserMessageType.scormSettingsNoPermission);
                            }
                            else
                            {
                                permissions = View.GetLinkPermissions(link, idCurrentUser);
                                idCommunity = link.SourceItem.CommunityID;
                            }
                        }
                        if (permissions != null && (permissions.Edit || permissions.EditSettings || permissions.ViewMyStatistics || permissions.ViewOtherStatistics))
                        {
                            uAction = ModuleRepository.ActionType.ScormSettingsView;
                            if (version.Availability == ItemAvailability.waitingsettings)
                            {
                                View.DisplayMessage(version.DisplayName, version.Extension, version.Type, Domain.UserMessageType.scormSettingsWaitingToSet);
                            }
                            dtoScormPackageSettings settings = Service.ScormPackageGetDtoCompletionSettings(idItem, idVersion, idSettings);
                            if (settings == null)
                            {
                                View.DisplayMessage(version.DisplayName, version.Extension, version.Type, Domain.UserMessageType.scormSettingsNotFound);
                            }
                            else
                            {
                                View.IdSettings = settings.Id;
                                View.LoadSettings(item.UniqueIdVersion.ToString(), item.DisplayName, settings, true, true);
                            }
                        }
                        else
                        {
                            uAction = ModuleRepository.ActionType.ScormSettingsNoPermissions;
                            View.DisplayMessage(version.DisplayName, version.Extension, version.Type, Domain.UserMessageType.scormSettingsNoPermission);
                        }
                    }
                    else
                    {
                        uAction = ModuleRepository.ActionType.ScormSettingsStatusError;
                        View.DisplayMessage(version.DisplayName, version.Extension, version.Type, Domain.UserMessageType.scormSettingsInvalidStatus, version.Availability);
                    }
                }

                View.RepositoryIdentifier  = CacheKeys.RepositoryIdentifierKey(rIdentifier.Type, rIdentifier.IdCommunity);
                View.RepositoryIdCommunity = rIdentifier.IdCommunity;
                View.RepositoryType        = rIdentifier.Type;
                View.IsInitialized         = true;
            }
            View.SendUserAction(idCommunity, Service.GetIdModule(), uAction);
        }