Ejemplo n.º 1
0
        public static void ApplyPermissions(ClientContext clientContext, Web web)
        {
            clientContext.Load(web, w => w.Url);
            clientContext.ExecuteQuery();

            string webVisitor = String.Format("{0}s", VisitorName);

            // check to see if group has already been created and assigned to this sub-site
            int groupId = -1;

            try
            {
                groupId = web.GetGroupID(webVisitor);
            }
            catch
            {
            }

            if (groupId != -1)
            {
                return;
            }


            Console.WriteLine("Applying custom permissions to {0}", web.Url);

            web.AddGroup(webVisitor, VisitorName, true);
            web.AddPermissionLevelToGroup(webVisitor, VisitorName);

            string webContributor = String.Format("{0} {1}s", web.Title, ContentContributorName);

            web.AddGroup(webContributor, ContentContributorName, true);
            web.AddPermissionLevelToGroup(webContributor, ContentContributorName);

            string webManager = String.Format("{0} {1}s", web.Title, ContentManagerName);

            web.AddGroup(webManager, ContentManagerName, true);
            web.AddPermissionLevelToGroup(webManager, ContentManagerName);


            web.Update();

            clientContext.ExecuteQuery();
        }
Ejemplo n.º 2
0
        public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "AddExternalUser")] HttpRequestMessage req, TraceWriter log)
        {
            try
            {
                // Gets data from request body.
                log.Info("Starting...");
                dynamic data = await req.Content.ReadAsAsync <object>();

                string siteUrl      = data.SiteUrl;
                string currentEmail = data.CurrentUser_EmailAddress;
                string groupName    = data.GroupName;
                log.Info(siteUrl);
                log.Info(currentEmail);
                log.Info(groupName);
                if (String.IsNullOrEmpty(siteUrl) || String.IsNullOrEmpty(currentEmail))
                {
                    return(req.CreateResponse(HttpStatusCode.BadRequest, "Please pass parametes site URL and Email Address in request body!"));
                }

                // Fetches client id and client secret from app settings.
                string clientId     = Environment.GetEnvironmentVariable("ClientId", EnvironmentVariableTarget.Process);
                string clientSecret = Environment.GetEnvironmentVariable("ClientSecret", EnvironmentVariableTarget.Process);
                string urlAdminSite = Environment.GetEnvironmentVariable("UrlAdminSite", EnvironmentVariableTarget.Process);

                // Obtains client context using the client id and client secret.
                //var ctx = new OfficeDevPnP.Core.AuthenticationManager().GetAppOnlyAuthenticatedContext(urlAdminSite, clientId, clientSecret);

                //Tenant tenant = new Tenant(ctx);
                //SiteProperties siteProps = tenant.GetSitePropertiesByUrl(siteUrl, true);
                //log.Info("a");
                //ctx.Load(siteProps);
                //log.Info("after tenant:" + siteUrl);
                //ctx.ExecuteQuery();
                //log.Info("after line " + siteUrl);

                var newctx = new OfficeDevPnP.Core.AuthenticationManager().GetAppOnlyAuthenticatedContext(siteUrl, clientId, clientSecret);

                Site site = newctx.Site;
                Web  web  = site.RootWeb;
                log.Info("get web");
                Group group = null;
                RoleDefinitionCollection permissionLevels = web.RoleDefinitions;

                log.Info("permissionLevels");

                newctx.Load(web);
                newctx.Load(web.SiteGroups);

                newctx.Load(permissionLevels);
                log.Info("execute query");
                newctx.ExecuteQuery();

                log.Info("after newctx");

                //if (CheckUserDomainFrom(siteProps, currentEmail))
                //{
                // If group doesn't exist in web, add it
                if (!GroupExistsInWebSite(web, groupName))
                {
                    if (groupName == "SCJ External Contribute")
                    {
                        //var permissionLevelExist = permissionLevels.Select(p => p.Name == "SCJ External Contribute").Count();
                        // Create Custom Permission Level
                        //if (permissionLevelExist!=0)
                        CreateContributePermissionLevel(web);
                        // Create new Group
                        group = AddGroup(web, groupName);
                        // Add Custom Pemission Level to Group
                        web.AddPermissionLevelToGroup(groupName, "SCJ External Contribute", true);
                    }

                    if (groupName == "SCJ External Read")
                    {
                        // Create Custom Permission Level
                        CreateReadPermissionLevel(web);
                        // Create new Group
                        group = AddGroup(web, groupName);
                        // Add Custom Pemission Level to Group
                        web.AddPermissionLevelToGroup(groupName, "SCJ External Read", true);
                    }
                }
                else     // Just Add the user to group
                {
                    group = web.SiteGroups.GetByName(groupName);
                }
                newctx.ExecuteQuery();
                return(req.CreateResponse(HttpStatusCode.OK, true));
                //}

                // return req.CreateResponse(HttpStatusCode.OK, false);
            }
            catch (Exception e)
            {
                return(req.CreateResponse(HttpStatusCode.InternalServerError, e.Message));
            }
        }