#if COPYRIGHT //------------------------------------------------------------------------------ // // Copyright (c) Microsoft Corporation. All rights reserved. // //------------------------------------------------------------------------------ #endif Sys.Services._RoleService = function() { /// Sys.Services._RoleService.initializeBase(this); this._roles = []; } Sys.Services._RoleService.DefaultWebServicePath = ''; Sys.Services._RoleService.prototype = { _defaultLoadCompletedCallback: null, _rolesIndex: null, _timeout: 0, _path: '', get_defaultLoadCompletedCallback: function() { /// /// Default callback to call when loading is complete. return this._defaultLoadCompletedCallback; }, set_defaultLoadCompletedCallback: function(value) { this._defaultLoadCompletedCallback = value; }, get_path: function() { /// Path to a role webservice. // override from base to ensure returned value is '' even if usercode sets to null, consistent with other appservices in v1. return this._path || ''; }, get_roles: function() { /// /// An array of role names for the current user. return Array.clone(this._roles); }, isUserInRole: function(role) { /// Gets a value indicating whether the current user is in the specified role. /// The name of the role to search in. /// var v = this._get_rolesIndex()[role.trim().toLowerCase()]; return !!v; }, load: function(loadCompletedCallback, failedCallback, userContext) { /// /// Callback to call when loading has completed. /// Callback to call if loading fails. /// Custom context passed to the completed or failed callback. Sys.Net.WebServiceProxy.invoke( this._get_path(), "GetRolesForCurrentUser", false, {} /* no params*/, Function.createDelegate(this, this._onLoadComplete), Function.createDelegate(this, this._onLoadFailed), [loadCompletedCallback, failedCallback, userContext], this.get_timeout()); }, _get_path: function() { var path = this.get_path(); if(!path || !path.length) { path = Sys.Services._RoleService.DefaultWebServicePath; } if(!path || !path.length) { throw Error.invalidOperation(Sys.Res.servicePathNotSet); } return path; }, _get_rolesIndex: function() { if (!this._rolesIndex) { var index = {}; for(var i=0; i < this._roles.length; i++) { index[this._roles[i].toLowerCase()] = true; } this._rolesIndex = index; } return this._rolesIndex; }, _onLoadComplete: function(result, context, methodName) { if(result && !(result instanceof Array)) { throw Error.invalidOperation(String.format(Sys.Res.webServiceInvalidReturnType, methodName, "Array")); } this._roles = result; this._rolesIndex = null; var callback = context[0] || this.get_defaultLoadCompletedCallback() || this.get_defaultSucceededCallback(); if (callback) { var userContext = context[2] || this.get_defaultUserContext(); var clonedResult = Array.clone(result); callback(clonedResult, userContext, "Sys.Services.RoleService.load"); } }, _onLoadFailed: function(err, context, methodName) { var callback = context[1] || this.get_defaultFailedCallback(); if (callback) { var userContext = context[2] || this.get_defaultUserContext(); callback(err, userContext, "Sys.Services.RoleService.load"); } #if DEBUG else { Sys.Net.WebServiceProxy._defaultFailedCallback(err, methodName); } #endif } } Sys.Services._RoleService.registerClass('Sys.Services._RoleService', Sys.Net.WebServiceProxy); Sys.Services.RoleService = new Sys.Services._RoleService();