Skip to content

Commit

Permalink
Merge pull request #187 from gunjandatta/gdatta
Browse files Browse the repository at this point in the history
Added hasPermissions Helper Method
  • Loading branch information
gunjandatta authored Nov 8, 2018
2 parents 641b84d + 3dace3f commit c444f0b
Show file tree
Hide file tree
Showing 13 changed files with 126 additions and 14 deletions.
32 changes: 32 additions & 0 deletions build/helper/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,38 @@ exports.createDocSet = function (name, listName, webUrl) {
}, reject);
});
};
/**
* Determines if the user has permissions, based on the permission kind value
*/
exports.hasPermissions = function (permissionMask, permissions) {
if (permissions === void 0) { permissions = []; }
var hasPermissions = false;
// Set the permissions
var requestedPermissions = typeof (permissions) === "number" ? [permissions] : permissions;
// Ensure the base permissions exist
if (SP && SP.BasePermissions) {
// Initialize the base permissions from the value
var basePermissions = new SP.BasePermissions();
basePermissions.initPropertiesFromJson(permissionMask);
// Default the permission flag
hasPermissions = true;
// Parse the requested permissions
for (var i = 0; i < requestedPermissions.length; i++) {
// See if the user has permissions
if (!basePermissions.has(requestedPermissions[i])) {
// Set the flag and break from the loop
hasPermissions = false;
break;
}
}
}
else {
// Log
console.info("[gd-sprest] The 'SP' core library is not available.");
}
// Return the value
return hasPermissions;
};
/**
* Convert a JSON string to a base object
*/
Expand Down
2 changes: 2 additions & 0 deletions build/mapper/sptypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ exports.BasePermissionTypes = {
ManagePersonalViews: 10,
ManageLists: 12,
ViewFormPages: 13,
AnonymousSearchAccessList: 14,
Open: 17,
ViewPages: 18,
AddAndCustomizePages: 19,
Expand All @@ -32,6 +33,7 @@ exports.BasePermissionTypes = {
AddDelPrivateWebParts: 29,
UpdatePersonalWebParts: 30,
ManageWeb: 31,
AnonymousSearchAccessWebLists: 32,
UseClientIntegration: 37,
UseRemoteAPIs: 38,
ManageAlerts: 39,
Expand Down
3 changes: 2 additions & 1 deletion build/rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var Mapper = require("./mapper");
* SharePoint REST Library
*/
exports.$REST = {
__ver: 4.35,
__ver: 4.36,
AppContext: function (siteUrl) { return Lib.Site.getAppContext(siteUrl); },
ContextInfo: Lib.ContextInfo,
DefaultRequestToHostFl: false,
Expand All @@ -18,6 +18,7 @@ exports.$REST = {
createDocSet: Helper.createDocSet,
Executor: Helper.Executor,
FieldSchemaXML: Helper.FieldSchemaXML,
hasPermissions: Helper.hasPermissions,
JSLink: Helper.JSLink,
ListForm: Helper.ListForm,
ListFormField: Helper.ListFormField,
Expand Down
17 changes: 17 additions & 0 deletions dist/gd-sprest.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ declare module 'gd-sprest' {
}

declare module 'gd-sprest/helper/types/helper' {
import { SP } from "gd-sprest-def";
import * as HelperTypes from "gd-sprest/helper/types";
import * as MapperTypes from "gd-sprest/mapper/types";
import * as UtilTypes from "gd-sprest/utils/types";
Expand Down Expand Up @@ -70,6 +71,11 @@ declare module 'gd-sprest/helper/types/helper' {
*/
FieldSchemaXML: (fieldInfo: HelperTypes.IFieldInfo) => PromiseLike<string>;

/**
* Determines if the user has permissions, based on the permission kind value
*/
hasPermissions(permissionMask: SP.BasePermissions, permissions: Array<number> | number): boolean;

/**
* Helper class for implementing JSLink solutions
*/
Expand Down Expand Up @@ -4805,6 +4811,8 @@ declare module 'gd-sprest/mapper/types/socialFeed' {
}

declare module 'gd-sprest/mapper/types/sptypes' {
import { SP } from "gd-sprest-def";

/**
* Base Permission Types
*/
Expand All @@ -4813,6 +4821,8 @@ declare module 'gd-sprest/mapper/types/sptypes' {
AddAndCustomizePages: number,
AddDelPrivateWebParts: number,
AddListItems: number,
AnonymousSearchAccessList: number,
AnonymousSearchAccessWebLists: number,
ApplyStyleSheets: number,
ApplyThemeAndBorder: number,
ApproveItems: number,
Expand Down Expand Up @@ -4847,6 +4857,13 @@ declare module 'gd-sprest/mapper/types/sptypes' {
ViewVersions: number
}

/**
* Base Permission Result
*/
export interface IGetUserEffectivePermissionsResult {
GetUserEffectivePermissions: SP.BasePermissions
}

/**
* Calendar Types
*/
Expand Down
6 changes: 3 additions & 3 deletions dist/gd-sprest.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/gd-sprest.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gd-sprest",
"version": "4.3.5",
"version": "4.3.6",
"description": "An easy way to develop against the SharePoint REST API.",
"author": "Gunjan Datta <[email protected]> (https://gunjandatta.github.io)",
"license": "<LICENSE>",
Expand Down
36 changes: 36 additions & 0 deletions src/helper/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Web } from "../lib";
import { IListItemResult } from "../mapper/types";
import { Base } from "../utils";
import { IRequest } from "./types";
declare var SP;

/**
* Creates a document set item.
Expand Down Expand Up @@ -53,6 +54,41 @@ export const createDocSet = (name: string, listName: string, webUrl?: string): P
});
}

/**
* Determines if the user has permissions, based on the permission kind value
*/
export const hasPermissions = (permissionMask: any, permissions: Array<number> | number = []): boolean => {
let hasPermissions = false;

// Set the permissions
let requestedPermissions = typeof (permissions) === "number" ? [permissions] : permissions;

// Ensure the base permissions exist
if (SP && SP.BasePermissions) {
// Initialize the base permissions from the value
let basePermissions = new SP.BasePermissions();
basePermissions.initPropertiesFromJson(permissionMask);

// Default the permission flag
hasPermissions = true;

// Parse the requested permissions
for (let i = 0; i < requestedPermissions.length; i++) {
// See if the user has permissions
if (!basePermissions.has(requestedPermissions[i])) {
// Set the flag and break from the loop
hasPermissions = false;
break;
}
}
} else {
// Log
console.info("[gd-sprest] The 'SP' core library is not available.");
}

// Return the value
return hasPermissions;
}

/**
* Convert a JSON string to a base object
Expand Down
6 changes: 6 additions & 0 deletions src/helper/types/helper.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { SP } from "gd-sprest-def";
import * as HelperTypes from ".";
import * as MapperTypes from "../../mapper/types";
import * as UtilTypes from "../../utils/types";
Expand Down Expand Up @@ -36,6 +37,11 @@ export interface IHelper {
*/
FieldSchemaXML: (fieldInfo: HelperTypes.IFieldInfo) => PromiseLike<string>;

/**
* Determines if the user has permissions, based on the permission kind value
*/
hasPermissions(permissionMask: SP.BasePermissions, permissions: Array<number> | number): boolean;

/**
* Helper class for implementing JSLink solutions
*/
Expand Down
2 changes: 2 additions & 0 deletions src/mapper/sptypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const BasePermissionTypes: SPTypes.IBasePermissionTypes = {
ManagePersonalViews: 10,
ManageLists: 12,
ViewFormPages: 13,
AnonymousSearchAccessList: 14,
Open: 17,
ViewPages: 18,
AddAndCustomizePages: 19,
Expand All @@ -32,6 +33,7 @@ export const BasePermissionTypes: SPTypes.IBasePermissionTypes = {
AddDelPrivateWebParts: 29,
UpdatePersonalWebParts: 30,
ManageWeb: 31,
AnonymousSearchAccessWebLists: 32,
UseClientIntegration: 37,
UseRemoteAPIs: 38,
ManageAlerts: 39,
Expand Down
11 changes: 11 additions & 0 deletions src/mapper/types/sptypes.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { SP } from "gd-sprest-def";

/**
* Base Permission Types
*/
Expand All @@ -6,6 +8,8 @@ export type IBasePermissionTypes = {
AddAndCustomizePages: number,
AddDelPrivateWebParts: number,
AddListItems: number,
AnonymousSearchAccessList: number,
AnonymousSearchAccessWebLists: number,
ApplyStyleSheets: number,
ApplyThemeAndBorder: number,
ApproveItems: number,
Expand Down Expand Up @@ -40,6 +44,13 @@ export type IBasePermissionTypes = {
ViewVersions: number
}

/**
* Base Permission Result
*/
export interface IGetUserEffectivePermissionsResult {
GetUserEffectivePermissions: SP.BasePermissions
}

/**
* Calendar Types
*/
Expand Down
3 changes: 2 additions & 1 deletion src/rest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { IREST } from "./rest.d";
* SharePoint REST Library
*/
export const $REST: IREST = {
__ver: 4.35,
__ver: 4.36,
AppContext: (siteUrl: string) => { return Lib.Site.getAppContext(siteUrl); },
ContextInfo: Lib.ContextInfo,
DefaultRequestToHostFl: false,
Expand All @@ -18,6 +18,7 @@ export const $REST: IREST = {
createDocSet: Helper.createDocSet,
Executor: Helper.Executor,
FieldSchemaXML: Helper.FieldSchemaXML,
hasPermissions: Helper.hasPermissions,
JSLink: Helper.JSLink,
ListForm: Helper.ListForm,
ListFormField: Helper.ListFormField,
Expand Down
18 changes: 11 additions & 7 deletions test/test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import {
$REST,
Helper
} from "../dist/gd-sprest";
$REST
} from "../src";

$REST.Web().getUserEffectivePermissions("").execute(r => {
let h = r.GetUserEffectivePermissions.High;
});

$REST.List("").execute(l => {l.EffectiveBasePermissions})

$REST.Search().postquery({
Querytext: "*",
Properties: [
{
Key: "GraphQuery",
Value: "ACTOR(ME,action:1013)",
ValueType: "string"
Name: "GraphQuery",
Value: { StrVal: "ACTOR(ME,action:1013)" }
}
]
});
Expand All @@ -20,7 +24,7 @@ $REST.Helper.SPConfig({
{ name: "Location", title: "Location", type: $REST.Helper.SPCfgFieldType.Geolocation },
],
ListInformation: {
TemplateType: 100,
BaseTemplate: 100,
Title: "Map"
}
}]
Expand Down

0 comments on commit c444f0b

Please sign in to comment.