Postingan lainnya
Hak akses user untuk menu di android studio
Ass, saya ingin membuat hak akses user untuk 1 menu pada aplikasi android saya.. pada php dan di database saat login user sudah ada 1 flag yang dinamakan flag "claim" menandakan bahwa user ini boleh mengakses menu ini atau tidak..bila di database user login ini flag claim nya "Y" maka user boleh masuk ke menu ini, sebaliknya bila flag claim nya "N" user tersebut tidak bisa masuk ke menu tsb.
untuk menu nya sendiri sudah ada. tinggal bagaimana cara agar hak akses user nya bisa berjalan..
berikut adalah script di homeactivity.java...dimana menu R.id.nav_claim yg akan di setting privilege nya..
public class HomeActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener, PolicyFragment.OnFragmentInteractionListener {
TextView navAgentName, navAgentCode;
String AgentHolder, AgentNameHolder;
TextView textMain;
LinearLayout layoutHome;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Intent intent = getIntent();
Bundle extras = intent.getExtras();
AgentHolder = extras.getString("KodeAgent");
AgentNameHolder = extras.getString("AgentName");
/*FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});*/
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
View headerView = navigationView.getHeaderView(0);
navAgentName = (TextView)headerView.findViewById(R.id.navAgentName);
navAgentCode = (TextView)headerView.findViewById(R.id.navAgentCode);
navAgentName.setText(AgentNameHolder);
navAgentCode.setText(AgentHolder);
/*layoutHome = (LinearLayout)findViewById(R.id.layoutHome);
textMain = (TextView)findViewById(R.id.textMain);
textMain.setText(AgentNameHolder);*/
GreetingFragment greetingFragment = GreetingFragment.newInstance(AgentNameHolder, "Param2");
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction txn = manager.beginTransaction();
txn.replace(R.id.frameContainer, greetingFragment);
txn.commit();
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home, menu);
return false;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
//layoutHome.setVisibility(View.GONE);
if (id == R.id.nav_policy) {
PolicyFragment policyFragment = PolicyFragment.newInstance(AgentHolder);
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction()
.addToBackStack(null)
.replace(R.id.frameContainer, policyFragment)
.commit();
} else if (id == R.id.nav_renewal) {
RenewalFragment renewalFragment = RenewalFragment.newInstance(AgentHolder, "Param2");
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction()
.addToBackStack(null)
.replace(R.id.frameContainer, renewalFragment)
.commit();
} else if (id == R.id.nav_today) {
TodayFragment todayFragment = TodayFragment.newInstance(AgentHolder, "Param2");
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction()
.addToBackStack(null)
.replace(R.id.frameContainer, todayFragment)
.commit();
} else if (id == R.id.nav_claim) {
ClaimFragment claimFragment = ClaimFragment.newInstance(AgentHolder);
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction()
.addToBackStack(null)
.replace(R.id.frameContainer, claimFragment)
.commit();
} else if (id == R.id.nav_logout) {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
Toast.makeText(this, "Logout Successfully", Toast.LENGTH_LONG).show();
//session.destroySession();
finish();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
@Override
public void onFragmentInteraction(String data) {
//Toast.makeText(this, data, Toast.LENGTH_SHORT).show();
}
}
berikut gambar ketika user login dan membawa flag nya <a href=' https://imgur.com/x1jzgBo '>Gambar</a>
1 Jawaban:
Saat user berhasil login, simpan status "claim" yang dari database ke sharedPreference android. Lalu di HomeActivity lakukan pengecekan "claim" yang disimpan di sharedPreference tadi, kalau "N" disabled/hidden menu claim agar tidak bisa diakses dan sebaliknya.