PasswordAuthorize.cs 9.11 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
using FLY.OBJComponents.Client;
using FLY.OBJComponents.IService;
using FLY.Thick.Base.Common;
using FLY.Thick.Base.IService;
using FObjBase;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FLY.Thick.Base.UI
{

    public class PasswordAuthorize:INotifyPropertyChanged
    {
        const string JSONDIST_KEY = "password";
21 22 23
        /// <summary>
        /// 服务器
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
24 25 26 27 28 29
        IJsonDistService jsonDist;
        /// <summary>
        /// 数据库
        /// </summary>
        [PropertyChanged.DoNotCheckEquality]
        public PasswordJsonDb Db { get; private set; } = new PasswordJsonDb();
30 31

        PasswordJsonDb DbDefault;
潘栩锋's avatar
潘栩锋 committed
32
        /// <summary>
33
        /// 修改时间,这个是服务器保存数据的时间。一切以服务器为准
潘栩锋's avatar
潘栩锋 committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
        /// </summary>
        public DateTime Time { get; set; }

        public PasswordAuthorize(IJsonDistService jsonDist)
        {
            this.jsonDist = jsonDist;
            Load();
            if (jsonDist is FObjServiceClient)
            {
                var objServiceClient = jsonDist as FObjServiceClient;
                if (objServiceClient.IsConnected)
                {
                    //连接成功
                    fetch_db();
                }
                objServiceClient.PropertyChanged += ObjServiceClient_PropertyChanged;
            }
            else 
            {
                //运行在服务器,之间获取数据
                fetch_db();
            }

            jsonDist.ValueChanged += JsonDist_ValueChanged;

        }

        private void ObjServiceClient_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "IsConnected") 
            {
                var objServiceClient = sender as FObjServiceClient;
                if (objServiceClient.IsConnected)
                {
                    //连接成功
                    fetch_db();
                }
            }
        }

        private void JsonDist_ValueChanged(object sender, JsonDistValueValueChangedEventArgs e)
        {
            if (e.key == JSONDIST_KEY) 
            {
                fetch_db();
            }
        }

82 83

        void check_db() 
潘栩锋's avatar
潘栩锋 committed
84
        {
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
            if (DbDefault == null)
                return;
            if (DbDefault.UiLvs.Count == 0)
                return;

            if (Db == null)
                Db = new PasswordJsonDb();

            // 界面权限
            foreach (var uiLv in DbDefault.UiLvs) {
                if (!Db.UiLvs.Any(u => u.UiName == uiLv.UiName)) {
                    Db.UiLvs.Add(new UiLvCell() { UiName = uiLv.UiName, Level = uiLv.Level, Description = uiLv.Description });
                }
            }

            //密码
            if (Db.Pws.Count() == 0) {
                foreach (var pw in DbDefault.Pws)
                {
                    Db.Pws.Add(new PasswordCell() { Password = pw.Password, Level=pw.Level });
                }
            }
潘栩锋's avatar
潘栩锋 committed
107 108 109 110 111 112 113 114 115 116 117 118
        }
        /// <summary>
        /// 从服务器获取数据
        /// </summary>
        void fetch_db() 
        {
            //连接成功
            this.jsonDist.GetValue(JSONDIST_KEY, (asyncContext, retData) =>
            {
                if (retData == null)
                {
                    //没有数据
119
                    //什么都不做
潘栩锋's avatar
潘栩锋 committed
120 121 122 123 124 125 126
                }
                else 
                {
                    var distCell = (DistCell)retData;
                    var p = (distCell.Value).ToObject<PasswordJsonDb>();
                    if (p == null || p.Pws==null || p.Pws.Count()==0)//异常
                    {
127
                        //什么都不做
潘栩锋's avatar
潘栩锋 committed
128 129 130
                    }
                    else 
                    {
131
                        //只要时间与服务器的不一样,全部覆盖!!
潘栩锋's avatar
潘栩锋 committed
132 133
                        if (Time != distCell.Time) 
                        {
134
                            Time = distCell.Time;
潘栩锋's avatar
潘栩锋 committed
135
                            Db = p;
136
                            check_db();
潘栩锋's avatar
潘栩锋 committed
137
                            Save();
138
                            
潘栩锋's avatar
潘栩锋 committed
139 140 141 142 143 144 145 146 147 148 149 150
                        }
                    }
                }
            }, this);
        }
        /// <summary>
        /// 推送数据到服务器
        /// </summary>
        void push_db(PasswordJsonDb db) 
        {
            this.jsonDist.SetValue(JSONDIST_KEY, JObject.FromObject(db));
        }
151

潘栩锋's avatar
潘栩锋 committed
152 153 154
        /// <summary>
        /// 输入密码,确认密码权限是否满足要求, level越大,要求权限越大
        /// </summary>
155 156 157
        /// <param name="pw">密码</param>
        /// <param name="level">要求级别</param>
        /// <param name="pw_lv">输入密码的级别</param>
潘栩锋's avatar
潘栩锋 committed
158
        /// <returns></returns>
159
        public AUTHORIZE_RESULT Authorize(string pw, int level, out int pw_lv)
潘栩锋's avatar
潘栩锋 committed
160
        {
161
            pw_lv = 0;
潘栩锋's avatar
潘栩锋 committed
162 163 164 165 166 167
            if (level <= 0)
                return AUTHORIZE_RESULT.OK;

            var v = from p in Db.Pws where p.Password == pw select p;
            if (v.Count() > 0)
            {
168
                pw_lv = v.First().Level;
潘栩锋's avatar
潘栩锋 committed
169 170 171 172 173 174 175 176 177 178 179 180 181
                if (v.First().Level >= level)
                    return AUTHORIZE_RESULT.OK;
                else
                    return AUTHORIZE_RESULT.ERR_LEVEL;
            }
            return AUTHORIZE_RESULT.ERR_PW;
        }
        /// <summary>
        /// 输入密码,确认密码权限是否满足要求, level越大,要求权限越大
        /// </summary>
        /// <param name="pw"></param>
        /// <param name="uiName">界面名称</param>
        /// <returns></returns>
182
        public AUTHORIZE_RESULT Authorize(string pw, string uiName, out int pw_lv) 
潘栩锋's avatar
潘栩锋 committed
183
        {
184
            return Authorize(pw, GetLv(uiName),out pw_lv);
潘栩锋's avatar
潘栩锋 committed
185 186 187 188 189 190 191 192 193
        }
        /// <summary>
        /// 获取授权级别, 0级不需要密码就能通过
        /// </summary>
        /// <param name="uiName"></param>
        /// <returns></returns>
        public int GetLv(string uiName) 
        {
            Base.Common.UiLvCell uiLvCell;
194 195
            uiLvCell = Db.UiLvs.Find(c => c.UiName == uiName);
            if (uiLvCell == null)
潘栩锋's avatar
潘栩锋 committed
196 197 198 199 200 201 202 203 204
            {
                uiLvCell = new Base.Common.UiLvCell()
                {
                    UiName = uiName
                };

                //没有,添加
                Db.UiLvs.Add(uiLvCell);
            }
205
            
潘栩锋's avatar
潘栩锋 committed
206 207 208 209
            return uiLvCell.Level;
        }

        string file_path = "password.json";
210
        string file_default_path = "default/password.default.json";
潘栩锋's avatar
潘栩锋 committed
211 212 213 214
        public event PropertyChangedEventHandler PropertyChanged;

        public void Apply(PasswordJsonDb db) 
        {
215 216
            //设置jsonDist,不需要设置到本地。 
            //jsonDist内的参数被修改后,会推送数据出来。到时再获取。
潘栩锋's avatar
潘栩锋 committed
217 218 219 220 221 222 223 224 225
            push_db(db);
        }
        void Save()
        {
            PasswordJsonDb2 p = new PasswordJsonDb2
            {
                Db = Db,
                Time = Time
            };
226
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(p, Newtonsoft.Json.Formatting.Indented);
潘栩锋's avatar
潘栩锋 committed
227 228 229 230 231 232 233 234 235
            try
            {
                File.WriteAllText(file_path, json);
            }
            catch
            {

            }
        }
236 237 238 239 240 241 242 243 244 245 246
        void LoadDefault() {
            if (!File.Exists(file_default_path))
                return;
            string json = File.ReadAllText(file_default_path);
            try
            {
                var p = Newtonsoft.Json.JsonConvert.DeserializeObject<PasswordJsonDb>(json);
                DbDefault = p;
            }
            catch
            {
潘栩锋's avatar
潘栩锋 committed
247

248 249
            }
        }
潘栩锋's avatar
潘栩锋 committed
250 251
        void Load()
        {
252 253 254
            LoadDefault();


潘栩锋's avatar
潘栩锋 committed
255
            if (!File.Exists(file_path))
256 257
            {
                check_db();
潘栩锋's avatar
潘栩锋 committed
258
                return;
259
            }
260
            
潘栩锋's avatar
潘栩锋 committed
261

262
            string json = File.ReadAllText(file_path);
潘栩锋's avatar
潘栩锋 committed
263 264 265 266 267
            try
            {
                var p = Newtonsoft.Json.JsonConvert.DeserializeObject<PasswordJsonDb2>(json);
                Db = p.Db;
                Time = p.Time;
268
                check_db();
潘栩锋's avatar
潘栩锋 committed
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
            }
            catch {
            
            }
        }
    }

    /// <summary>
    /// 授权输入返回结果
    /// </summary>
    public enum AUTHORIZE_RESULT
    {
        /// <summary>
        /// 成功
        /// </summary>
        OK,
        /// <summary>
        /// 不够级别
        /// </summary>
        ERR_LEVEL,
        /// <summary>
        /// 密码出错
        /// </summary>
        ERR_PW
    }


    public class PasswordJsonDb2
    {
298 299 300
        /// <summary>
        /// 与服务器同步参数
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
301
        public PasswordJsonDb Db;
302

潘栩锋's avatar
潘栩锋 committed
303 304 305 306 307 308 309 310 311 312
        /// <summary>
        /// 修改时间
        /// </summary>
        public DateTime Time;
    }
    public class PasswordJsonDb
    {
        /// <summary>
        /// 界面授权列表
        /// </summary>
313
        public List<UiLvCell> UiLvs { get; } = new List<UiLvCell>();
潘栩锋's avatar
潘栩锋 committed
314 315 316 317

        /// <summary>
        /// 密码列表
        /// </summary>
318
        public List<PasswordCell> Pws { get; } = new List<PasswordCell>();
潘栩锋's avatar
潘栩锋 committed
319 320 321
    }

}