zoukankan      html  css  js  c++  java
  • SPGroup 和SPUser的常用操作

    http://www.cnblogs.com/gzh4455/archive/2012/03/26/2417854.html

    private bool RemoveUserFromGroup(string sGoupName, string sUserLoginName)
        {
            bool res = false;
            try
            {
                SPWeb web = SPContext.Current.Web;
                SPGroup oGroup = web.SiteGroups[sGoupName];
                SPUser oUser = GetSPUser(sUserLoginName);
                if (oUser != null)
                {
                    web.AllowUnsafeUpdates = true;
                    oGroup.RemoveUser(oUser);
                    oGroup.Update();
                    res = true;
                    web.AllowUnsafeUpdates = false;
                }
            }
            catch (Exception ex)
            {
                string sMessage = ex.Message;
                //throw;
            }
            return res;
        }
        private bool RemoveUserFromGroup(SPGroup oGroup, SPUser oUser)
        {
            bool res = false;
            try
            {
                SPWeb web = SPContext.Current.Web;
                if (oUser != null&&oGroup!=null)
                {
                    web.AllowUnsafeUpdates = true;
                    oGroup.RemoveUser(oUser);
                    oGroup.Update();
                    res = true;
                    web.AllowUnsafeUpdates = false;
                }
            }
            catch (Exception ex)
            {
                string sMessage = ex.Message;
                //throw;
            }
            return res;
        }
        private SPUser GetSPUser(string sLoginName)
        {
            SPUser oUser = null;
            try
            {
                if (!string.IsNullOrEmpty(sLoginName))
                {
                    oUser = SPContext.Current.Web.EnsureUser(sLoginName);
                }
            }
            catch (Exception ex)
            {
                string sMessage = ex.Message;
            }
            return oUser;
        }
    
        private void RemoveUser(string sLoginName)
        {
            SPUser oUser = GetSPUser(sLoginName);
            if (oUser!=null)
            {
                SPGroupCollection groups = oUser.Groups;
                if (groups!=null&&groups.Count>0)
                {
                    foreach (SPGroup g in groups)
                    {
                        RemoveUserFromGroup(g, oUser);
                    }
                }
            }
        }
    
    
        private bool AddUserIntoGroup(string sGroupName, string sUserLoginName)
        {
            bool res = false;
            try
            {
                SPWeb web = SPContext.Current.Web;
                web.AllowUnsafeUpdates = true;
                SPGroup oGroup = web.SiteGroups[sGroupName];
                SPUser oUser = GetSPUser(sUserLoginName);
                if (oUser != null)
                {
                    oGroup.AddUser(oUser);
                    oGroup.Update();
    
                    res = true;
                }
                web.AllowUnsafeUpdates = false;
            }
            catch (Exception ex)
            {
                string sMessage = ex.Message;
                //throw;
            }
            return res;
        }
    
        private string FilterSPUserString(string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return str;
            }
            if (str.IndexOf(";#") > 0)
            {
                str = str.Substring(str.LastIndexOf(";#") + 2);
            }
            return str;
        }
    
    
        private bool CreateSiteGroup(string sGroupName, string sGroupDescription)
        {
            bool res = false;
            using (SPWeb oWebsiteRoot = SPContext.Current.Site.RootWeb)
            {
                oWebsiteRoot.AllowUnsafeUpdates = true;
                SPGroupCollection collGroups = oWebsiteRoot.SiteGroups;
                string sLoginName = SPContext.Current.Web.CurrentUser.LoginName;
                SPUser oUser = oWebsiteRoot.Users[sLoginName];
                SPMember oMember = oWebsiteRoot.Users[sLoginName];
                collGroups.Add(sGroupName, oMember, oUser, "Description");
                oWebsiteRoot.AllowUnsafeUpdates = false;
                res = true;
            }
            return res;
        }
        /// <summary>
        /// 给组赋权限
        /// </summary>
        /// <param name="sGroupName"></param>
        /// <param name="sPermissionLever"></param>
        /// <returns></returns>
        private bool SetGroupPermission(string sGroupName, string sPermissionLever)
        {
            bool res = false;
            using (SPWeb oWebsiteRoot = SPContext.Current.Site.RootWeb)
            {
                oWebsiteRoot.AllowUnsafeUpdates = true;
                SPRoleAssignment roleAssignment = new SPRoleAssignment(oWebsiteRoot.SiteGroups[sGroupName]);
                roleAssignment.RoleDefinitionBindings.Add(oWebsiteRoot.RoleDefinitions[sPermissionLever]);
                oWebsiteRoot.Update();
                oWebsiteRoot.AllowUnsafeUpdates = false;
                res = true;
            }
            return res;
        }
    
        private void DeleteSiteGroup(SPWeb web, string groupName)
        {
            web.AllowUnsafeUpdates = true;
            SPGroupCollection groups = web.SiteGroups;
            groups.Remove(groupName);
            web.Update();
            web.AllowUnsafeUpdates = false;
        }
  • 相关阅读:
    SpringCloud学习笔记(3)----Spring Cloud Netflix之深入理解Eureka
    SpringCloud学习笔记(2)----Spring Cloud Netflix之Eureka的使用
    SpringCloud学习笔记(1)----认识微服务与SpringCloud
    SpringBoot学习笔记(16)----SpringBoot整合Swagger2
    SpringBoot学习笔记(15)----SpringBoot使用Druid
    SpringBoot学习笔记(14)----应用监控-HTTP方式
    SpringBoot学习笔记(13)----使用Spring Session+redis实现一个简单的集群
    SpringBoot学习笔记(12)----SpringBoot实现多个 账号轮询发送邮件
    SpringBoot学习笔记(11)-----SpringBoot中使用rabbitmq,activemq消息队列和rest服务的调用
    SpringBoot学习笔记(8)-----SpringBoot文件上传
  • 原文地址:https://www.cnblogs.com/lishidefengchen/p/6114103.html
Copyright © 2011-2022 走看看