/// <summary> /// add permission for user /// </summary> /// <param name="_username"></param> /// <param name="_codePermit"></param> /// <param name="_usernameAdmin"></param> /// <returns></returns> public bool addPermission(string _username, string _codePermit, string _usernameAdmin) { //LTDHDataContext DB = new LTDHDataContext(@strPathDB); Permission permitDAO = new Permission(); try { using (TransactionScope ts = new TransactionScope()) { tblUser u = DB.tblUsers.Single(p => p.Username == _username.Trim()); if (u != null) { //user had this permisssion if (isAllow(u.Permission, _codePermit)) { ts.Complete(); return true; } else { if (!u.Permission.Trim().EndsWith(CommonConstants.COMMA)) { u.Permission += CommonConstants.COMMA; } u.Permission += permitDAO.getValue(_codePermit).ToString(); DB.SubmitChanges(); ts.Complete(); //write log log.writeLog(DBHelper.strPathLogFile, _usernameAdmin, BaseServices.createMsgByTemplate(CommonConstants.SQL_UPDATE_SUCCESSFUL_TEMPLATE, _username + CommonConstants.BAR + _codePermit, CommonConstants.SQL_TABLE_PERMISSION)); } } } } catch (Exception ex) { log.writeLog(DBHelper.strPathLogFile, _usernameAdmin, ex.Message); return false; } return false; }
/// <summary> /// Đăng ký user mới /// </summary> /// <param name="_username"></param> /// <param name="_displayName"></param> /// <param name="_email"></param> /// <param name="_sex"></param> /// <param name="_password"></param> /// <returns></returns> public Boolean register(string _username, string _displayName, string _email, Boolean _sex, string _password) { LTDHDataContext DB = new LTDHDataContext(@strPathDB); try { using (TransactionScope ts = new TransactionScope()) { tblUser user = new tblUser(); Permission permitDAO = new Permission(); user.Username = _username; user.DisplayName = _displayName; user.Sex = _sex; user.Email = _email; user.Password = encryptPassword(_password); user.Note = "Password: " + _password; user.Type = true; user.Permission = permitDAO.getValue(CommonConstants.P_N_GENERAL).ToString(); user.RegisterDate = DateTime.Today; user.NumberOfArticles = 0; user.State = CommonConstants.STATE_NON_ACTIVE; DB.tblUsers.InsertOnSubmit(user); DB.SubmitChanges(); ts.Complete(); log.writeLog(DBHelper.strPathLogFile, CommonConstants.USER_GUEST, BaseServices.createMsgByTemplate(CommonConstants.SQL_INSERT_SUCCESSFUL_TEMPLATE, _username, CommonConstants.SQL_TABLE_USER)); } } catch (Exception e) { log.writeLog(DBHelper.strPathLogFile, CommonConstants.USER_GUEST, BaseServices.createMsgByTemplate(CommonConstants.SQL_INSERT_FAILED_TEMPLATE, _username, CommonConstants.SQL_TABLE_USER)); log.writeLog(DBHelper.strPathLogFile, CommonConstants.USER_GUEST, e.Message); return false; } return true; }
/// <summary> /// check permission /// </summary> /// <param name="strPermission"></param> /// <param name="_codePermission"></param> /// <returns></returns> public bool isAllow(string strPermission, string _codePermission) { strPermission = BaseServices.nullToBlank(strPermission); if (!BaseServices.isNullOrBlank(_codePermission) && !BaseServices.isNullOrBlank(strPermission)) { Permission permitDAO = new Permission(); int p = permitDAO.getValue(_codePermission); string[] arrayPermit = strPermission.Split(CommonConstants.COMMA_CHAR); for (int i = 0; i < arrayPermit.Length; i++) { if (arrayPermit[i].Equals(p.ToString().Trim())) { return true; } } } return false; }