房间的Firebase创建者成为该房间的管理员

时间:2017-03-12 07:07:06

标签: firebase firebase-security

如标题中所述,我希望允许创建房间的用户成为该房间的管理员,允许他更新该房间的详细信息或邀请其他用户。我已经阅读了一些数据库安全规则教程我仍然不明白它是如何工作的。我发现的最接近的是这个链接:

Defining the User roles in Firebase

我尝试了它仍然无法工作,我是否需要这样做一张新桌子或什么?有人可以向我解释它是如何工作的,或者我如何实现它?My database

编辑1: 添加了主要和添加活动

编辑2: 添加了Team和TeamList类

AddActivity

public class AddActivity extends AppCompatActivity {

private FirebaseAuth firebaseAuth;

EditText editTextName;
Spinner spinnerSport;
Button buttonAddTeam;

List<Team> teams;

DatabaseReference databaseTeams;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add);

    firebaseAuth = FirebaseAuth.getInstance();

    if(firebaseAuth.getCurrentUser() == null){

        finish();

        startActivity(new Intent(this, SignInActivity.class));
    }

    databaseTeams = FirebaseDatabase.getInstance().getReference("teams");

    editTextName = (EditText) findViewById(R.id.editTextName);
    spinnerSport = (Spinner) findViewById(R.id.spinnerSports);

    buttonAddTeam = (Button) findViewById(R.id.buttonAddTeam);

    teams = new ArrayList<>();


    buttonAddTeam.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            addTeam();

            Intent myIntent = new Intent(AddActivity.this,
                    MainActivity.class);
            startActivity(myIntent);
        }
    });
}



private void addTeam() {

    String name = editTextName.getText().toString().trim();
    String sport = spinnerSport.getSelectedItem().toString();

    FirebaseUser user = firebaseAuth.getCurrentUser();
    user.getUid();

    if (!TextUtils.isEmpty(name)) {

        String id = databaseTeams.push().getKey();
        String owner = user.getUid();

        Team team = new Team(id, name, sport, owner);

        databaseTeams.child(id).setValue(team);

        editTextName.setText("");

        Toast.makeText(this, "Team added", Toast.LENGTH_LONG).show();
    } else {

        Toast.makeText(this, "Please enter a name", Toast.LENGTH_LONG).show();
    }
}
}

MainActivity

public class MainActivity extends AppCompatActivity {
public static final String TEAM_NAME = "com.example.user.stfv2.teamname";
public static final String TEAM_ID = "com.example.user.stfv2.teamid";

private FirebaseAuth firebaseAuth;

EditText editTextName;
Spinner spinnerSport;
Button buttonAddTeam;
ListView listViewTeams;


List<Team> teams;


DatabaseReference databaseTeams;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    firebaseAuth = FirebaseAuth.getInstance();


    if(firebaseAuth.getCurrentUser() == null){

        finish();

        startActivity(new Intent(this, SignInActivity.class));
    }


    FirebaseUser user = firebaseAuth.getCurrentUser();

    databaseTeams = FirebaseDatabase.getInstance().getReference("teams");


    editTextName = (EditText) findViewById(R.id.editTextName);
    spinnerSport = (Spinner) findViewById(R.id.spinnerSports);
    listViewTeams = (ListView) findViewById(R.id.listViewTeams);

    buttonAddTeam = (Button) findViewById(R.id.buttonAddTeam);


    teams = new ArrayList<>();


    buttonAddTeam.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent myIntent = new Intent(MainActivity.this,
                    AddActivity.class);
            startActivity(myIntent);
        }
    });


    listViewTeams.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            Team team = teams.get(i);

            Intent intent = new Intent(getApplicationContext(), TeamActivity.class);

            intent.putExtra(TEAM_ID, team.getTeamId());
            intent.putExtra(TEAM_NAME, team.getTeamName());


            startActivity(intent);
        }
    });

    listViewTeams.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
            Team team = teams.get(i);
            showUpdateDeleteDialog(team.getTeamId(), team.getTeamName());
            return true;
        }
    });


}

private void showUpdateDeleteDialog(final String teamId, String teamName) {

    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
    LayoutInflater inflater = getLayoutInflater();
    final View dialogView = inflater.inflate(R.layout.update_dialog, null);
    dialogBuilder.setView(dialogView);

    final EditText editTextName = (EditText) dialogView.findViewById(R.id.editTextName);
    final Spinner spinnerSport = (Spinner) dialogView.findViewById(R.id.spinnerSports);
    final Button buttonUpdate = (Button) dialogView.findViewById(R.id.buttonUpdateTeam);
    final Button buttonDelete = (Button) dialogView.findViewById(R.id.buttonDeleteTeam);

    dialogBuilder.setTitle(teamName);
    final AlertDialog b = dialogBuilder.create();
    b.show();


    buttonUpdate.setOnClickListener(new View.OnClickListener() {

        FirebaseUser user = firebaseAuth.getCurrentUser();

        @Override
        public void onClick(View view) {
            String name = editTextName.getText().toString().trim();
            String sport = spinnerSport.getSelectedItem().toString();
            String owner = user.getUid();

            if (!TextUtils.isEmpty(name)) {
                updateTeam(teamId, name, sport, owner);
                b.dismiss();
            }
        }
    });


    buttonDelete.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            deleteTeam(teamId);
            b.dismiss();
        }
    });
}

private boolean updateTeam(String id, String name, String sport, String owner) {

    DatabaseReference dR = FirebaseDatabase.getInstance().getReference("teams").child(id);


    Team team = new Team(id, name, sport, owner);
    dR.setValue(team);
    Toast.makeText(getApplicationContext(), "Team Updated", Toast.LENGTH_LONG).show();
    return true;
}

private boolean deleteTeam(String id) {

    DatabaseReference dR = FirebaseDatabase.getInstance().getReference("teams").child(id);


    dR.removeValue();


    DatabaseReference drTracks = FirebaseDatabase.getInstance().getReference("tracks").child(id);


    drTracks.removeValue();
    Toast.makeText(getApplicationContext(), "Team Deleted", Toast.LENGTH_LONG).show();

    return true;
}

@Override
protected void onStart() {
    super.onStart();

    databaseTeams.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {


            teams.clear();


            for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {

                Team team = postSnapshot.getValue(Team.class);

                teams.add(team);
            }


            TeamList teamAdapter = new TeamList(MainActivity.this, teams);

            listViewTeams.setAdapter(teamAdapter);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
        case R.id.action_logout:

            firebaseAuth.signOut();

            finish();

            startActivity(new Intent(this, SignInActivity.class));
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}


}

Team.java:

package com.example.user.stfv2;

import com.google.firebase.database.IgnoreExtraProperties;

@IgnoreExtraProperties
public class Team {
private String teamId;
private String teamName;
private String teamSport;
private String teamOwner;


public Team(String teamId, String teamName, String teamSport, String teamOwner) {
    this.teamId = teamId;
    this.teamName = teamName;
    this.teamSport = teamSport;
    this.teamOwner = teamOwner;
}

public String getTeamId() {return teamId;}

public String getTeamName() {
    return teamName;
}

public String getTeamSport() {
    return teamSport;
}
//this method is never used, i don't know where to use it
public String getTeamOwner() { return  teamOwner; }
}

TeamList.java

public class TeamList extends ArrayAdapter<Team> {
private Activity context;
List<Team> teams;

public TeamList(Activity context, List<Team> teams) {
    super(context, R.layout.layout_team_list, teams);
    this.context = context;
    this.teams = teams;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();
    View listViewItem = inflater.inflate(R.layout.layout_team_list, null, true);

    TextView textViewName = (TextView) listViewItem.findViewById(R.id.textViewName);
    TextView textViewSport = (TextView) listViewItem.findViewById(R.id.textViewSport);

    Team team = teams.get(position);
    textViewName.setText(team.getTeamName());
    textViewSport.setText(team.getTeamSport());

    return listViewItem;
}
}

1 个答案:

答案 0 :(得分:1)

如果您的房间只有一个管理员,您可以使用它。 . . .是创建房间的用户的uid。

db结构:

$uid

遵守以下规则:

root: {
  sportsteamfinder: {
    teams: {
      $team: {
        teamId: $team,
        teamOwner: $uid,
        teamName: henq,
        teamSport: Football
      }
    }
  }
}

或者,如果您想要每个房间有多个管理员。再次使用$ uid和$ uid2作为管理员的ID。

db结构:

{
  "rules": {
    "sportsteamfinder": {
      "teams": {
        "$team": {
          ".write": "data.child('teamOwner').val() === auth.uid || !data.exists()"
        }
      }
    }
  }
}

遵守以下规则:

root: {
  sportsteamfinder: {
    teams: {
      $team: {
        teamId: $team,
        teamAdmins: {
          $uid: true,
          $uid2: true
        },
        teamName: henq,
        teamSport: Football
      }
    }
  }
}