/// <summary>抓取远程头像</summary> /// <param name="user"></param> /// <param name="url"></param> /// <returns></returns> public virtual Boolean FetchAvatar(IManageUser user, String url = null) { if (url.IsNullOrEmpty()) { url = user.GetValue("Avatar") as String; } //if (av.IsNullOrEmpty()) throw new Exception("用户头像不存在 " + user); // 尝试从用户链接获取头像地址 if (url.IsNullOrEmpty() || !url.StartsWithIgnoreCase("http")) { var list = UserConnect.FindAllByUserID(user.ID); url = list.OrderByDescending(e => e.UpdateTime) .Where(e => !e.Avatar.IsNullOrEmpty() && e.Avatar.StartsWithIgnoreCase("http")) .FirstOrDefault()?.Avatar; } if (url.IsNullOrEmpty()) { return(false); } if (!url.StartsWithIgnoreCase("http")) { return(false); } // 不要扩展名 var set = Setting.Current; var dest = set.AvatarPath.CombinePath(user.ID + ".png").GetBasePath(); //// 头像是否已存在 //if (File.Exists(dest)) return false; LogProvider.Provider?.WriteLog(user.GetType(), "抓取头像", $"{url} => {dest}", user.ID, user + ""); dest.EnsureDirectory(true); try { //var wc = new WebClientX(); //Task.Factory.StartNew(() => wc.DownloadFileAsync(url, av)).Wait(5000); var client = new HttpClient(); var rs = client.GetAsync(url).Result; var buf = rs.Content.ReadAsByteArrayAsync().Result; File.WriteAllBytes(dest, buf); // 更新头像 user.SetValue("Avatar", "/Sso/Avatar/" + user.ID); (user as IEntity)?.Update(); return(true); } catch (Exception ex) { XTrace.WriteException(ex); } return(false); }
/// <summary>填充用户,登录成功并获取用户信息之后</summary> /// <param name="client"></param> /// <param name="user"></param> protected virtual void Fill(OAuthClient client, IManageUser user) { client.Fill(user); var dic = client.Items; // 用户信息 if (dic != null && user is User user2) { if (user2.Code.IsNullOrEmpty()) { user2.Code = client.UserCode; } if (user2.Mobile.IsNullOrEmpty()) { user2.Mobile = client.Mobile; } if (user2.Mail.IsNullOrEmpty()) { user2.Mail = client.Mail; } if (user2.Sex == SexKinds.未知 && dic.TryGetValue("sex", out var sex)) { user2.Sex = (SexKinds)sex.ToInt(); } if (user2.Remark.IsNullOrEmpty()) { user2.Remark = client.Detail; } var set = Setting.Current; // 使用认证中心的角色 if (set.UseSsoRole) { var roleId = GetRole(dic, true); if (roleId > 0) { user2.RoleID = roleId; var ids = GetRoles(client.Items, true).ToList(); if (ids.Contains(roleId)) { ids.Remove(roleId); } if (ids.Count == 0) { user2.RoleIds = null; } else { user2.RoleIds = "," + ids.OrderBy(e => e).Join() + ","; } } } // 使用本地角色 if (user2.RoleID <= 0 && !set.DefaultRole.IsNullOrEmpty()) { user2.RoleID = Role.GetOrAdd(set.DefaultRole).ID; } // 部门 if (set.UseSsoDepartment && !client.DepartmentCode.IsNullOrEmpty() && !client.DepartmentName.IsNullOrEmpty()) { var dep = Department.FindByCode(client.DepartmentCode); if (dep == null) { dep = new Department { Code = client.DepartmentCode, Name = client.DepartmentName, Enable = true }; dep.Insert(); } user2.DepartmentID = dep.ID; } // 头像。有可能是相对路径,需要转为绝对路径 var av = client.Avatar; if (av != null && av.StartsWith("/") && client.Server.StartsWithIgnoreCase("http")) { av = new Uri(new Uri(client.Server), av) + ""; } if (user2.Avatar.IsNullOrEmpty()) { user2.Avatar = av; } // 本地头像,如果不存在,也要更新 else if (user2.Avatar.StartsWithIgnoreCase("/Sso/Avatar/", "/Sso/Avatar?")) { var av2 = Setting.Current.AvatarPath.CombinePath(user2.ID + ".png").GetBasePath(); if (!File.Exists(av2)) { LogProvider.Provider?.WriteLog(user.GetType(), "更新头像", true, $"{user2.Avatar} => {av}", user.ID, user + ""); user2.Avatar = av; } } // 下载远程头像到本地,Avatar还是保存远程头像地址 if (user2.Avatar.StartsWithIgnoreCase("http") && !set.AvatarPath.IsNullOrEmpty()) { Task.Run(() => FetchAvatar(user, av)); } } }
/// <summary>填充用户,登录成功并获取用户信息之后</summary> /// <param name="client"></param> /// <param name="user"></param> protected virtual void Fill(OAuthClient client, IManageUser user) { client.Fill(user); var dic = client.Items; // 用户信息 if (dic != null && user is User user2) { if (user2.Code.IsNullOrEmpty()) { user2.Code = client.UserCode; } if (user2.Mobile.IsNullOrEmpty()) { user2.Mobile = client.Mobile; } if (user2.Mail.IsNullOrEmpty()) { user2.Mail = client.Mail; } if (user2.Sex == SexKinds.未知 && client.Sex != 0) { user2.Sex = (SexKinds)client.Sex; } if (user2.Remark.IsNullOrEmpty()) { user2.Remark = client.Detail; } var set = Setting.Current; var roleId = 0; List <Int32> roleIds = null; // 使用认证中心的角色 if (set.UseSsoRole) { // 跟本地系统角色合并 var sys = user2.Roles.Where(e => e.IsSystem).Select(e => e.ID).ToList(); if (sys.Count > 0) { roleId = user2.RoleID; if (roleIds == null) { roleIds = new List <Int32>(); } roleIds.AddRange(sys); } roleId = GetRole(dic, true); if (roleId > 0) { user2.RoleID = roleId; var ids = GetRoles(client.Items, true).ToList(); if (roleIds == null) { roleIds = new List <Int32>(); } roleIds.AddRange(ids); } } // 使用本地角色 if (user2.RoleID <= 0 && !set.DefaultRole.IsNullOrEmpty()) { user2.RoleID = roleId = Role.GetOrAdd(set.DefaultRole).ID; } // OAuth提供者的自动角色 var cfg = OAuthConfig.FindAllWithCache().FirstOrDefault(e => e.Name.EqualIgnoreCase(client.Name)); if (cfg != null && !cfg.AutoRole.IsNullOrEmpty()) { var ids = GetRoles(cfg.AutoRole, true).ToList(); if (roleIds == null) { roleIds = new List <Int32>(); } roleIds.AddRange(ids); } if (roleIds != null) { roleIds = roleIds.Distinct().ToList(); if (roleIds.Contains(roleId)) { roleIds.Remove(roleId); } if (roleIds.Count == 0) { user2.RoleIds = null; } else { user2.RoleIds = "," + roleIds.OrderBy(e => e).Join() + ","; } } // 部门 if (set.UseSsoDepartment && !client.DepartmentCode.IsNullOrEmpty() && !client.DepartmentName.IsNullOrEmpty()) { var dep = Department.FindByCode(client.DepartmentCode); if (dep == null) { dep = new Department { Code = client.DepartmentCode, Name = client.DepartmentName, Enable = true }; dep.Insert(); } user2.DepartmentID = dep.ID; } // 头像。有可能是相对路径,需要转为绝对路径 var av = client.Avatar; if (av != null && av.StartsWith("/") && client.Server.StartsWithIgnoreCase("http")) { av = new Uri(new Uri(client.Server), av) + ""; } if (user2.Avatar.IsNullOrEmpty()) { user2.Avatar = av; } // 本地头像,如果不存在,也要更新 else if (user2.Avatar.StartsWithIgnoreCase("/Sso/Avatar/", "/Sso/Avatar?")) { var av2 = Setting.Current.AvatarPath.CombinePath(user2.ID + ".png").GetBasePath(); if (!File.Exists(av2)) { LogProvider.Provider?.WriteLog(user.GetType(), "更新头像", true, $"{user2.Avatar} => {av}", user.ID, user + ""); user2.Avatar = av; } } // 下载远程头像到本地,Avatar还是保存远程头像地址 if (user2.Avatar.StartsWithIgnoreCase("http") && !set.AvatarPath.IsNullOrEmpty()) { Task.Run(() => FetchAvatar(user, av)); } } }