/// <summary> /// Looks up roles of the authenticated user and selects a user view model type accordingly. /// </summary> /// <param name="controllerContext">Controller context.</param> /// <param name="bindingContext">Binding context.</param> /// <returns>Specific user view model, based on the highest role.</returns> /// <exception cref="Exception">Thrown when no user identity is found in the HTTP context.</exception> public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { if (bindingContext.ModelType != typeof(IUserViewModel)) { throw new Exception($"The model is not of type {nameof(IUserViewModel)}."); } else { var user = UserManager.FindByNameAsync(controllerContext.HttpContext.User?.Identity?.Name).Result; if (user == null) { throw new Exception($"The {nameof(IUserViewModel)} model cannot be bound because the user could not be retrieved."); } else { var userRoles = UserManager.GetRolesAsync(user.Id).Result.ToMedioClinicRoles(); // The roles should be evaluated from the highest to the lowest. if (FlagEnums.HasAnyFlags(Roles.Doctor, userRoles)) { bindingContext.ModelMetadata.Model = new DoctorViewModel(); } else if (FlagEnums.HasAnyFlags(Roles.Patient, userRoles)) { bindingContext.ModelMetadata.Model = new PatientViewModel(); } return(DefaultModelBinder.BindModel(controllerContext, bindingContext)); } } }
public void KeyboardClass_Tests() { Assert.True(FlagEnums.HasAnyFlags(DeviceCapabilityClass.Keyboard, DeviceCapability.KeyA.GetClass())); Assert.False(FlagEnums.HasAnyFlags(DeviceCapabilityClass.Controller, DeviceCapability.KeyA.GetClass())); Assert.True(FlagEnums.HasAnyFlags(DeviceCapabilityClass.KeyboardMouse, DeviceCapability.KeyA.GetClass())); Assert.False(FlagEnums.HasAnyFlags(DeviceCapabilityClass.Mouse, DeviceCapability.KeyA.GetClass())); }
public void ControllerClass_Tests() { Assert.True(FlagEnums.HasAnyFlags(DeviceCapabilityClass.Controller, DeviceCapability.Button0.GetClass())); Assert.True(FlagEnums.HasAnyFlags(DeviceCapabilityClass.Controller, DeviceCapability.Axis0.GetClass())); Assert.True(FlagEnums.HasAnyFlags(DeviceCapabilityClass.Controller, DeviceCapability.Hat0E.GetClass())); Assert.True(FlagEnums.HasAnyFlags(DeviceCapabilityClass.ControllerDirectional, DeviceCapability.Hat0E.GetClass())); Assert.True(FlagEnums.HasAnyFlags(DeviceCapabilityClass.ControllerFaceButton, DeviceCapability.Button0.GetClass())); Assert.True(FlagEnums.HasAnyFlags(DeviceCapabilityClass.ControllerAxis, DeviceCapability.Axis0.GetClass())); }
public void MouseClass_Tests() { Assert.True(FlagEnums.HasAnyFlags(DeviceCapabilityClass.Mouse, DeviceCapability.Mouse0.GetClass())); Assert.True(FlagEnums.HasAnyFlags(DeviceCapabilityClass.MouseButton, DeviceCapability.Mouse0.GetClass())); Assert.False(FlagEnums.HasAnyFlags(DeviceCapabilityClass.MouseCursor, DeviceCapability.Mouse0.GetClass())); Assert.True(FlagEnums.HasAnyFlags(DeviceCapabilityClass.Mouse, DeviceCapability.CursorX.GetClass())); Assert.True(FlagEnums.HasAnyFlags(DeviceCapabilityClass.MouseCursor, DeviceCapability.CursorX.GetClass())); Assert.False(FlagEnums.HasAnyFlags(DeviceCapabilityClass.MouseButton, DeviceCapability.CursorX.GetClass())); }
private static object GetEnums(this Type enumType, object value) { if (FlagEnums.IsFlagEnum(enumType) && FlagEnums.HasAnyFlags(enumType, value)) { return(string.Join("|", FlagEnums.GetFlagMembers(enumType, value) .Select(member => $"{enumType.FullName}.{member.Name}"))); } var name = Enum.GetName(enumType, value); return($"{enumType.FullName}.{name}"); }
public InputTemplate(IControllerElementMappingProfile mappedElements, int playerIndex = 0) { this.PlayerIndex = playerIndex; ProxyGenerator generator = new ProxyGenerator(); this._Options = (from prop in typeof(T).GetProperties() let inputOptionAttribute = prop.GetCustomAttribute <InputOptionAttribute>() where inputOptionAttribute != null let name = prop.Name select(name, option: (IInputOption) new InputOption(inputOptionAttribute, name))) .ToDictionary(o => o.name, o => o.option); var overrides = (from element in mappedElements from key in this._Options.Keys let option = this._Options[key] let target = option.TargetElement where element.LayoutElement == target where FlagEnums.HasAnyFlags(option.OptionType, element.DeviceCapability.GetClass()) select(key, element.DeviceCapability)).ToDictionary(d => d.key, d => d.DeviceCapability); var map = from key in this._Options.Keys let value = overrides.ContainsKey(key) ? overrides[key] : DeviceCapability.None select new KeyValuePair <string, DeviceCapability>(key, value); //this.configurationOptions = (from prop in typeof(T).GetProperties() // let configAttribute = prop.GetCustomAttribute<ConfigurationOptionAttribute>() // where configAttribute != null // let name = prop.Name // let metadata = prop.GetCustomAttributes<CustomMetadataAttribute>() // select new ConfigurationOptionDescriptor(configAttribute, metadata, name) as IConfigurationOptionDescriptor).ToList(); this.ValueCollection = new ConfigurationValueCollection(); var configDescriptor = new ConfigurationSectionDescriptor <T>(typeof(T).Name); ((ConfigurationValueCollection)this.ValueCollection).EnsureSectionDefaults(configDescriptor); this.inputTemplateInterceptor = new InputTemplateInterceptor <T>(map.ToDictionary(m => m.Key, m => m.Value), this.ValueCollection, configDescriptor); var circular = new InputTemplateCircularInterceptor <T>(this); this.Configuration = new InputConfigurationSection <T>(circular, this.inputTemplateInterceptor); this.Template = generator.CreateInterfaceProxyWithoutTarget <T>(circular, this.inputTemplateInterceptor); }
public override void OnAuthorization(AuthorizationContext authorizationContext) { if (authorizationContext == null) { throw new ArgumentNullException(nameof(authorizationContext)); } var user = HttpContext.Current.User; if (user != null) { var userRoles = UserInfoProvider.GetRolesForUser(user.Identity?.Name, SiteName).ToMedioClinicRoles(); if (user.Identity?.IsAuthenticated == false || !FlagEnums.HasAnyFlags(Roles, userRoles)) { // Call a framework method designed for such cases. HandleUnauthorizedRequest(authorizationContext); } } }
/// <inheritdoc/> public DeviceCapability this[ControllerElement virtualElement] { set { IEnumerable <string> optionKeys = from option in this._Options where option.Value.TargetElement == virtualElement where FlagEnums.HasAnyFlags(option.Value.OptionType, value.GetClass()) select option.Key; if (!optionKeys.Any()) { throw new KeyNotFoundException( "This template does not support the target element or element type."); } foreach (string optionKey in optionKeys) { this.inputTemplateInterceptor.InputValues[optionKey] = value; } } }
public void OnAuthorization(AuthorizationFilterContext context) { if (context is null) { throw new ArgumentNullException(nameof(context)); } var user = context.HttpContext?.User; if (user != null) { var userRoles = UserInfoProvider.GetRolesForUser(user.Identity?.Name, SiteContext.CurrentSiteName).ToMedioClinicRoles(); if (user.Identity?.IsAuthenticated == false || !FlagEnums.HasAnyFlags(Roles, userRoles)) { context.Result = new StatusCodeResult((int)System.Net.HttpStatusCode.Forbidden); return; } } }
public InputConfiguration(IControllerElementMappingProfile mappedElements, int playerIndex = 0) { this.PlayerIndex = playerIndex; this.Descriptor = ConfigurationDescriptorCache.GetSectionDescriptor <T>(typeof(T).Name); this.Options = (from prop in typeof(T).GetProperties() let inputOptionAttribute = prop.GetCustomAttribute <InputOptionAttribute>() where inputOptionAttribute != null let name = prop.Name select(name, option: (IInputOption) new InputOption(inputOptionAttribute, name))) .ToDictionary(o => o.name, o => o.option); var overrides = (from element in mappedElements from key in this.Options.Keys let option = this.Options[key] let target = option.TargetElement where element.LayoutElement == target where FlagEnums.HasAnyFlags(option.OptionType, element.DeviceCapability.GetClass()) select(key, element.DeviceCapability)).ToDictionary(d => d.key, d => d.DeviceCapability); var map = from key in this.Options.Keys let value = overrides.ContainsKey(key) ? overrides[key] : DeviceCapability.None select new KeyValuePair <string, DeviceCapability>(key, value); this.ValueCollection = new ConfigurationValueCollection(); var genInstance = typeof(T).GetCustomAttribute <ConfigurationGenerationInstanceAttribute>(); if (genInstance == null) { throw new InvalidOperationException("Not generated!"); // todo: mark with interface to fail at compile time. } this.Configuration = (T)Instantiate.CreateInstance(genInstance.InstanceType, new[] { typeof(IConfigurationSectionDescriptor), typeof(IConfigurationValueCollection), typeof(Dictionary <string, DeviceCapability>) }, Expression.Constant(this.Descriptor), Expression.Constant(this.ValueCollection), Expression.Constant(map.ToDictionary(m => m.Key, m => m.Value))); this.ConfigurationSection = new ConfigurationSection <T>(this.ValueCollection, this.Descriptor, this.Configuration); }